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]
{
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]
{