473,666 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1996
just preparing for 70-320 so i will try and answer some questions.

if you remotable class inherits from MarshalByRefern ce 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**@thisisjun k.beverli.net> wrote in message
news:Oi******** ******@TK2MSFTN GP09.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**@thisisjun k.beverli.net> wrote in message
news:Oi******** ******@TK2MSFTN GP09.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 MarshalByRefern ce 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**@thisisjun k.beverli.net> wrote in message
news:ey******** *****@TK2MSFTNG P11.phx.gbl...
Hey Hermit..

Hermit Dave wrote:
if you remotable class inherits from MarshalByRefern ce 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**@thisisjun k.beverli.net> wrote in message
news:em******** ******@TK2MSFTN GP14.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**@thisisjun k.beverli.net> wrote in
news:Oi******** ******@TK2MSFTN GP09.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.Proxie s;
using System.Security .Permissions;
using System.Runtime. Remoting.Contex ts;

[AttributeUsage( AttributeTarget s.Class)]
[SecurityPermiss ionAttribute(Se curityAction.De mand,
Flags=SecurityP ermissionFlag.I nfrastructure)]
public class MyProxyAttribut e : ProxyAttribute
{
public override MarshalByRefObj ect CreateInstance( Type serverType)
{
if (serverType == typeof(CustomSe rver))
return new DerivedClass();
else
return base.CreateInst ance(serverType );
}
}

[MyProxyAttribut e]
public class CustomServer : ContextBoundObj ect
{
public virtual void HelloMethod()
{
Console.WriteLi ne("Base method");
}
}

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

class Test
{
static void Main()
{
CustomServer x = new CustomServer();
x.HelloMethod() ;
}
}
Jul 21 '05 #9
"Niki Estner" <ni*********@cu be.net> wrote in
news:ug******** ******@TK2MSFTN GP15.phx.gbl...
"Bjarke Lindberg" <bv**@thisisjun k.beverli.net> wrote in
news:Oi******** ******@TK2MSFTN GP09.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 RemotingService s.

Niki

using System;
using System.Reflecti on;
using System.Runtime. Remoting;
using System.Runtime. Remoting.Proxie s;
using System.Security .Permissions;

[AttributeUsage( AttributeTarget s.Class)]
[SecurityPermiss ionAttribute(Se curityAction.De mand,
Flags=SecurityP ermissionFlag.I nfrastructure)]
public class MyProxyAttribut e : ProxyAttribute
{
public override MarshalByRefObj ect CreateInstance( Type serverType)
{
if (serverType == typeof(CustomSe rver))
{
DerivedClass d = new DerivedClass();
return d;
}
else
{
MethodInfo mi =
typeof(Remoting Services).GetMe thod("AllocateI nitializedObjec t",
BindingFlags.St atic|BindingFla gs.NonPublic, null, new Type[] {
typeof(Type) }, null);
object o = mi.Invoke(null, new object[] { typeof(DerivedC lass) });
return (MarshalByRefOb ject)o;
}
}
}
[MyProxyAttribut e]
public class CustomServer : ContextBoundObj ect
{
public virtual void HelloMethod()
{
Console.WriteLi ne("Base method");
}
}

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

class Test
{
static unsafe void Main()
{
CustomServer x = new CustomServer();
x.HelloMethod() ;
}
}
Jul 21 '05 #10

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

Similar topics

4
2860
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; double s_id;
3
3052
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 the bottom. I found some scraps of code in various postings that I am trying to get to work. I can not seem to get any meaningful data back from the CompareFunc callback. According to MSDN, The lParam1 and lParam2 parameters correspond to the...
2
5541
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 th32ParentProcessID As Long
10
273
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 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
0
1140
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 marshalling on a function-by-function basis. Is there a way in .Net to marshall entire interfaces? Bill
2
1419
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
2485
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 GetChildren(GetChildrenParm *, Results *);
2
3341
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 need to pass an array of RADIO_INFO2 structures to be filled by a function in the DLL. This is how the structure is defined in the C++ example that comes with the DLL:
0
1154
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 module is taking to deal with the unmannaged data through dll and execution is much more. Is there any way to avoid this marshalling? I also wants to learn more about marshalling. Is there any link from where I can free download any ebook or...
0
8443
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8356
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8866
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8639
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7385
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6192
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2769
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1772
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.