C# Proxy

  • Thread starter Nicholas Paldino [.NET/C# MVP]
  • Start date
N

Nicholas Paldino [.NET/C# MVP]

David,

It's not a horrible idea, but with the tools that exist today, you can
do this without compiler support. Basically, you can create a tool that
will scan the metadata and create this code for you. If anything, I would
expect to see it in a refactoring.

Also, I'm a little worried about the syntax. It's a little archaic, and
I think would be frowned upon (but that's a personal inference, I could be
very wrong, I'm just going on what I know of the C# team and their way of
thinking).

One problem you don't address is what do you do if the method/property
is not declared as virtual? The methods end up being shadowed, and when you
cast to the base class, your proxy all of a sudden fails to work (and you
lose what you were trying to achieve in the first place).

For something like this, you might want to consider context bound
objects, which allow you to tag your class with attributes, and intercept
the calls that go into and out of the class. Check out the article in MSDN
magazine titled "Decouple Components by Injecting Custom Services into Your
Object's Interception Chain" by Juval Lowy, located at (watch for line
wrap):

http://msdn.microsoft.com/msdnmag/issues/03/03/contextsinnet/

It should give you a good idea on how to implement something like this
on your own, without having to generate all that code for each proxy class.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

"David Lojudice S." <dalssoft at gmail dot com> wrote in message
Hi,

I've been reading the Design Patterns (Addison-Wesley, 1995) and, on Proxy
chapter, I was thinking how difficult would be if I wanted to do a proxy for
a big class in C#.

Let's get a big class like Control, from System.WinForm. Imagine that I want
to create a proxy for this class to load an inner control (a private
variable) on demand. It could be for others propose, but let use the same
example used on Design Patterns. I want to use the proxy as the parent class
(Control), hiding the logic of loading on demand. The code would be
something like that:

using System;
using System.Windows.Forms;

public class ControlProxy: Control
{
private Control _control;

public override string Text
{
get
{
LoadInnerObject();
return _control.Text + " [by ControlProxy]";
}
}

private void LoadInnerObject()
{
if (_control == null)
_control = new Control("New Control");
}
}



To use ControlProxy:

ControlProxy cp = new ControlProxy();
string controlText;

controlText = cp.Text;



But, if I want a real proxy I have to override all methods, event,
properties, etc. to get ControlProxy accessing _control's variable and its
members, not its own members.

Well, the strange part of this post comes now.

I'm suggesting to create a new keyword on C# to compiler understands it is a
proxy and generates all the code necessary to have a class witch access an
internal variable and its members.

The code with the new keyword would be something like that:

public class ControlProxy: Control proxy[_control]
{
 
H

Helge Jensen

David said:
But, if I want a real proxy I have to override all methods, event,
properties, etc. to get ControlProxy accessing _control’s variable and
its members, not its own members.

Actually, this is done automatically when you use remoting of an
instance of MarshalByRef, and you can mimic that. You can use reflection
to dynamically create classes that will proxy calls.
Oh my god! I think I’m to far from earth. But it would be very nice to
see it working… :)

It shouldn't be *that* hard to write, but you would need to know a few
things about generating IL and comparing C# function signatures.
Anyway…. maybe there is a very easy answer to this pattern and I just
don’t know. If someone knows how to do this, please, share with me.

I may decide to do this myself, but it will probably take some time
before I have the time to do it, so don't hold your breath ;)
Ps: sorry about my English.

Your english is actually quite good, atlest if you are not a native speaker.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top