Intercepting object creation

  • Thread starter Thread starter Hugo Wetterberg
  • Start date Start date
H

Hugo Wetterberg

Hi all,
Is there any way to intercept an object creation and supply another object?
As when using proxy objects in remoting.

The scenario is that i have a class B that extends A, when new A() is
called I want to supply a B object.

Is this possible?

/Hugo
 
You should make A's ctor protected and B's ctor private and instead provide
a static factory method in class B like that:

public static A GetObject() { /*code here*/ }
 
You should make A's ctor protected and B's ctor private and instead provide
a static factory method in class B like that:

public static A GetObject() { /*code here*/ }

Yes, that would be a possible solution in some cases. But my application
compiles new classes at startup that are used to extend the functionality
of existing classes. It's a kind of scripting scheme. Instead of specifying
a interface betwen script and code, I've decided to put the script in the
code.

So there might or might not be any descendants to class A, or there might
be a whole inheritance chain.

When multiple scripts have been added that extends a single class...

A
B:A
C:A
D:A
E:A

....the code is restructured to form an inheriance chain...

A
B:A
C:B
D:C
E:D

....and in this case a instance of E will be provided when the app wants to
construct an object of the type A.

So the actual type of the object is not known at (application-)compile
time, only the base type is known.

I've already implemented this, but I would like a nicer construction than
the current...

ScriptingEngine.GetScriptedObject
(Type type, Type[] argTypes, object[] args)

So, what I'm looking for is the transparency, syntactic sugar turning the
above code into...

new A(a,b,c,...)

/Hugo
 
Back
Top