Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / February 2005

Tip: Looking for answers? Try searching our database.

Calling a private base constructor from within a constructor in MSIL

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Kemal Erdogan - 23 Jan 2005 10:58 GMT
Hello All,

I am trying to implement proxies for a certain group of classes, using
System.Reflection.Emit. Subclassing works fine as long as the base class
have a non-private default constructor.

Here is a part of my code:

ConstructorInfo ci = basetype.GetConstructor(ALL_INSTANCE_METHODS,
            null, CallingConventions.Standard,
            Type.EmptyTypes, new ParameterModifier[0] );

constructorIL.Emit( OpCodes.Ldarg_0 );
constructorIL.Emit( OpCodes.Call, ci );

peverify.exe complains when I generate a proxy assembly that makes calls
to private base constructor. And it actually does not work.

Is it possible at all to make a call to private base constructor? And a
weird question: Can I actually create the base class instance using
reflection and the base private constructor, then assign the returned
object to "this" pointer (the argument 0) within subclass's constructor.

Thank you for any answers,
Kemal
Signature

Using Opera's revolutionary e-mail client: http://www.opera.com/m2/

"Rick Byers [MSFT]" - 04 Feb 2005 21:50 GMT
Hi Kemal,
No you cannot call a private base class constructor directly - that is
exactly what the "private" means <grin>.  Also, in order for a constructor
to be verifiable, it must directly call a constructor of it's base class.  
Jim Miller's annotated CLI book has an interesting discussion (page 76) of
the implications of this for languages like Eiffel that don't want this
restriction.

Assuming you only want to run in full-trust, you might be able to get away
with something unverifiable.  For example, don't call any base constructor
directly, but instead use reflection to invoke the base constructor using
your existing "this" pointer.  However, I'm not sure offhand whether
reflection will let you invoke a constructor on an existing object like a
regular method.

Regardless, there are larger problems with creating a true transparent
proxy class like this yourself.  This is exactly why the CLR has built-in
support for transparent proxies.  Is there some reason why you couldn't use
the existing TransparentProxy system to do what you want?

Rick

--------
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
> Subject: Calling a private base constructor from within a constructor in
MSIL
> Message-ID: <opsk1x3jpe2wljbb@kk-home.chello.at>
> From: "Kemal Erdogan" <kemal@erdogan.biz>
[quoted text clipped - 25 lines]
> Thank you for any answers,
> Kemal
Kemal Erdogan - 06 Feb 2005 01:03 GMT
Hello Rick,

Thank you for your reply. I would need a custom solution
just because dotnet framework does not support multiple
inheritance.

I actually need to intercept method calls, so change of
method definitions at runtime would also be a possible
solution for me. Do you know a good tool, or have some
source code for that?

Kemal

>-----Original Message-----
>Hi Kemal,
[quoted text clipped - 55 lines]
>
>.
"Rick Byers [MSFT]" - 07 Feb 2005 19:35 GMT
Right, the class you are proxying would have to extend MarshalByRefObject
(or ContextBoundObject) in order to use a TransparentProxy to intercept
calls to it.  When the JIT compiler sees a non-virtual method call, it
needs to have the freedom to inline it which eliminates pretty much any
dynamic interception technique (MBROs are a special case).  For your
original emitted-subclass approach do you require the user to use your
proxy type directly?  If it is casted to the base class, interception of
non-vrituals no longer works.  If that is acceptable for you, then perhaps
you could use encapsulation instead of inheritance.

The CLR has no general-purpose run-time interception mechanism (probably
due in large part to the performance implications).  If this is what you
you're trying to achieve, you're probably out of luck.  The only
possibility would be to use the profiler API to replace code on the fly
when it is jitted (see [1]).  However, the profiler API is intended only to
be used in a development context, so depending on your exact scenario, it
is likely to be inappropriate.

Rick
[1] http://msdn.microsoft.com/msdnmag/issues/03/09/NETProfilingAPI/

--------
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
> From: "Kemal Erdogan" <kemal@erdogan.biz>
> Subject: RE: Calling a private base constructor from within a constructor
in MSIL
> Date: Sat, 5 Feb 2005 17:03:32 -0800
>
[quoted text clipped - 96 lines]
> >
> >.
Fabian Schmied - 08 Feb 2005 08:17 GMT
> I actually need to intercept method calls, so change of
> method definitions at runtime would also be a possible
> solution for me. Do you know a good tool, or have some
> source code for that?

I recently found Castle Project's DynamicProxy, which is more flexible  
than the built-in proxy support, maybe you'll find it useful:  
http://www.castleproject.org/castle/show/dynamicproxy

It works by creating dynamic proxy classes at runtime, either deriving  
from your class and overriding virtual methods, or implementing an  
interface and delegating to your own implementation. The first approach  
has the disadvantage of supporting virtual methods only, but it also  
allows interception of intra-object calls, which is a nice thing to have.

Fabian

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.