.NETGURU
Custom marshalling
Messages   Related Types
This message was discovered on microsoft.public.dotnet.general.

Post a new message to this list...

Bjarke Lindberg
Hey NG.

Using .NET remoting it's possible to "override" the <c>new</c> operator
to return a proxy to the object in question.

I'd like to use this approach to let a factory class return the
reference to the newly created object instead of letting the runtime
instantiate the object.

Does anybody know any references for doing this? (If it's possible at
all). Any feedback would be appriciated.

Thanks,
Bjarke

Reply to this message...
 
    
Nick Malik
Are you looking for references on how to create a factory object in C#?
This is a pretty good site.
http://www.dofactory.com/Patterns/Patterns.aspx

Are you looking for references on how to learn OO Design Patterns like the
Factory, and how to use them?
http://biztalkbum.blogspot.com/2004/07/how-to-learn-object-oriented.html

I hope this helps,
--- Nick

"Bjarke Lindberg" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Bjarke Lindberg
Hello Nick.

[Original message clipped]

Thanks for your response - please check my reply to Hermit Dave..

/Bjarke
Reply to this message...
 
    
Hermit Dave
just preparing for 70-320 so i will try and answer some questions.

if you remotable class inherits from MarshalByRefernce then when a client
tries to instantiate it... all they get is the proxy object.
the only thing is that remoting runtime instantiates the object. you can set
the mode to either client activated CAO... ie the lifetime on server is
determined by the client
or server activated SAO... in which case you can either have a single call
or a singleton implementation.

you can also choose to use a custom class factory which is a SAO and a
method will return the remotable object as client activated

more of this one
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconnetremotingoverview.asp

--

Regards,

Hermit Dave
(http://hdave.blogspot.com)
"Bjarke Lindberg" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Bjarke Lindberg
Hey Hermit..

Hermit Dave wrote:
[Original message clipped]

Right. And that is what my question is about. I know about remoting and
I know my (GOF) patterns - that's why I figured that it should be
possible to do what I initially asked. Maybe I wasn't clear enough, when
asking so I'll try again.

I need a way to make the runtime use a factory when creating a new
instance of a class. Like the remoting runtime is creating a new proxy,
and returning the reference to this, when you're using the <c>new</c>
operator on a class. (On a Client Activated Object)

For instance - I'm creating a framework. This framework have a factory
class which should/could be used to create instances of a class in this
framework. This means - everyone outsite the framework should use this
factory class when creating new instances and I could mark the
constructor of the classes as <c>internal</c>.

Anyway - wouldn't it be elegant if the user of the framework could just
create a new instance using <code>MyClass myClass = new MyClass()</code>
and then the .NET runtime would intercept the call and returning a
reference created by the factory? - Remoting is using this "feature"
when creating CAO's - and my question is - is it possible to plug
in/implement some type of custom marshaler and make this happen seamless
for the user of the framework?

/B.asking
Reply to this message...
 
    
Hermit Dave
well the way it currently works is that you get a proxy to the class factory
and then use a method on class factory to create an instance of the desired
CAO.
if your CAO has two constructor then write two methods on Class Factory or
overload the method.

for your users to just use
MyClass myClassInstance = new MyClass()
you need the objects to in the current namespace declaration. Unfortunately
with remoting objects they are not. I am not aware of hijacking the 'new'
keyword in .net framework (reminds me of something COM+ does - it hijacks
the call before delegating it to COM SCM.)

another thing if you make the classes as internal your users wont be able to
use the 'new' keyword.

CAOs with class factorys have a implementation similar to a Singleton in the
way that you have to call a method to get a instance. With singletons its
Static method of the Class with class factory. With remoting you execute a
public method or a property.

--

Regards,

Hermit Dave
(http://hdave.blogspot.com)
"Bjarke Lindberg" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Bjarke Lindberg
Hermit Dave wrote:

[Original message clipped]

Well - my intention wasn't to use remoting, but the same strategy as it
use to "hijack" the <c>new</c> operator. It doesn't seems to be
documented anywhere - maybe I could try to look into the mono sources
and see how they managed to do it there (if it's implemented).

Thanks for your response.

/Bjarke
Reply to this message...
 
    
Hermit Dave
good luck with that... you should get the mono source... alternatively you
look search around using rotor. MS implementation would be closer to the one
in Rotor than in mono.

--

Regards,

Hermit Dave
(http://hdave.blogspot.com)
"Bjarke Lindberg" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Niki Estner
"Bjarke Lindberg" <Click here to reveal e-mail address> wrote in
news:Click here to reveal e-mail address...
[Original message clipped]

Can you apply a custom attribute to that class? Then it's quite easy (see
code below).

Niki

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Proxies;
using System.Security.Permissions;
using System.Runtime.Remoting.Contexts;

[AttributeUsage(AttributeTargets.Class)]
[SecurityPermissionAttribute(SecurityAction.Demand,
Flags=SecurityPermissionFlag.Infrastructure)]
public class MyProxyAttribute : ProxyAttribute
{
public override MarshalByRefObject CreateInstance(Type serverType)
{
if (serverType == typeof(CustomServer))
return new DerivedClass();
else
return base.CreateInstance(serverType);
}
}

[MyProxyAttribute]
public class CustomServer : ContextBoundObject
{
public virtual void HelloMethod()
{
Console.WriteLine("Base method");
}
}

public class DerivedClass : CustomServer
{
public override void HelloMethod()
{
Console.WriteLine("Been derived!");
}
}

class Test
{
static void Main()
{
CustomServer x = new CustomServer();
x.HelloMethod();
}
}

Reply to this message...
 
    
Niki Estner
"Niki Estner" <Click here to reveal e-mail address> wrote in
news:Click here to reveal e-mail address...
[Original message clipped]

The code I posted before always returned a transparent proxy; The only way
I've found to create the real object without a proxy was using an
undocumented function from RemotingServices.

Niki

using System;
using System.Reflection;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Proxies;
using System.Security.Permissions;

[AttributeUsage(AttributeTargets.Class)]
[SecurityPermissionAttribute(SecurityAction.Demand,
Flags=SecurityPermissionFlag.Infrastructure)]
public class MyProxyAttribute : ProxyAttribute
{
public override MarshalByRefObject CreateInstance(Type serverType)
{
if (serverType == typeof(CustomServer))
{
DerivedClass d = new DerivedClass();
return d;
}
else
{
MethodInfo mi =
typeof(RemotingServices).GetMethod("AllocateInitializedObject",
BindingFlags.Static|BindingFlags.NonPublic, null, new Type[] {
typeof(Type) }, null);
object o = mi.Invoke(null, new object[] { typeof(DerivedClass) });
return (MarshalByRefObject)o;
}
}
}

[MyProxyAttribute]
public class CustomServer : ContextBoundObject
{
public virtual void HelloMethod()
{
Console.WriteLine("Base method");
}
}

public class DerivedClass : CustomServer
{
public override void HelloMethod()
{
Console.WriteLine("Been derived!");
}
}

class Test
{
static unsafe void Main()
{
CustomServer x = new CustomServer();
x.HelloMethod();
}
}

Reply to this message...
 
    
Bjarke Lindberg
Niki Estner wrote:
[Original message clipped]

Thanks Niki! You've saved my day! :)

/Bjarke
Reply to this message...
 
 
System.AttributeTargets
System.Console
System.ContextBoundObject
System.MarshalByRefObject
System.Reflection.BindingFlags
System.Reflection.MethodInfo
System.Runtime.Remoting.Proxies.ProxyAttribute
System.Runtime.Remoting.RemotingServices
System.Security.Permissions.SecurityAction
System.Security.Permissions.SecurityPermissionAttribute
System.Security.Permissions.SecurityPermissionFlag
System.Type




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification