473,395 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Custom marshalling

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

Jul 21 '05 #1
10 1973
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/de...ngoverview.asp

--

Regards,

Hermit Dave
(http://hdave.blogspot.com)
"Bjarke Lindberg" <bv**@thisisjunk.beverli.net> wrote in message
news:Oi**************@TK2MSFTNGP09.phx.gbl...
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

Jul 21 '05 #2
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/...-oriented.html

I hope this helps,
--- Nick

"Bjarke Lindberg" <bv**@thisisjunk.beverli.net> wrote in message
news:Oi**************@TK2MSFTNGP09.phx.gbl...
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

Jul 21 '05 #3
Hey Hermit..

Hermit Dave wrote:
if you remotable class inherits from MarshalByRefernce then when a client
tries to instantiate it... all they get is the proxy object.


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
Jul 21 '05 #4
Hello Nick.
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


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

/Bjarke
Jul 21 '05 #5
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" <bv**@thisisjunk.beverli.net> wrote in message
news:ey*************@TK2MSFTNGP11.phx.gbl...
Hey Hermit..

Hermit Dave wrote:
if you remotable class inherits from MarshalByRefernce then when a client
tries to instantiate it... all they get is the proxy object.


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

Jul 21 '05 #6
Hermit Dave wrote:
for your users to just use
MyClass myClassInstance = new MyClass()
you need the objects to in the current namespace declaration.


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
Jul 21 '05 #7
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" <bv**@thisisjunk.beverli.net> wrote in message
news:em**************@TK2MSFTNGP14.phx.gbl...
Hermit Dave wrote:
for your users to just use
MyClass myClassInstance = new MyClass()
you need the objects to in the current namespace declaration.


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

Jul 21 '05 #8
"Bjarke Lindberg" <bv**@thisisjunk.beverli.net> wrote in
news:Oi**************@TK2MSFTNGP09.phx.gbl...
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.


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();
}
}
Jul 21 '05 #9
"Niki Estner" <ni*********@cube.net> wrote in
news:ug**************@TK2MSFTNGP15.phx.gbl...
"Bjarke Lindberg" <bv**@thisisjunk.beverli.net> wrote in
news:Oi**************@TK2MSFTNGP09.phx.gbl...
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.


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


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("AllocateInitia lizedObject",
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();
}
}
Jul 21 '05 #10
Niki Estner wrote:
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.


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

/Bjarke
Jul 21 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Animesh | last post by:
Hi All, I don't know whethher this is possible or not. This is the result of a bad design problem. Here I go; I have a structure like this: typedef struct _s_index_entry { char *doc_id;...
3
by: Ed Sutton | last post by:
I need to do a custom sort on a TreeView. I have various object types associated with the TreeNode Tag property. I want to sort objects of the same type at the top of the list, other objects at...
2
by: Howard Kaikow | last post by:
I've got the following in a VB 6 project: Private Type PROCESSENTRY32 dwSize As Long cntUsage As Long th32ProcessID As Long th32DefaultHeapID As Long th32ModuleID As Long cntThreads As Long...
10
by: Bjarke Lindberg | last post by:
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...
0
by: swartzbill2000 | last post by:
I am familiar with the VB6/VC6/ATL way of marshalling an incoming interface via New (VB), and then marshalling an outgoing interface with some form of Advise. To me it looks like .Net has...
2
by: bonk | last post by:
I am currently trying to write a longer article about Marshalling when using . Does anyone know books, articles, or websites that cover Marshalling in Platform Invocation Services ()?
2
by: RJ Lohan | last post by:
Howdy, I have a legacy DLL for which I have a problem marshalling a parameter type of char**. The function header (C++) is as so; extern "C" __declspec(dllexport) int __stdcall...
2
by: calenlas | last post by:
Hi all, I'm taking my first steps into C# <--C++ DLL Interop and unfortunately I've run into (what seems to be) a very complicated case as my first task. Perhaps someone here can help me. I...
0
by: santoshdarekar | last post by:
HI, I have some code which I am reverse engineering. Here is one module which is used as calculation madule and marshalling is used in it. But, still there is performance issue as the CPU time the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.