Override classes/objects within a context?

  • Thread starter Thread starter Aquila Deus
  • Start date Start date
A

Aquila Deus

Hi all!

I wondering if there is any method to override object/class within a
context, like this:

// MyNullFileInfo is derived from FileInfo

let (System.IO.FileInfo = MyNullFileInfo) {
FileInfo fi = new FileInfo(); // create a MyNullFileInfo.
fi...... //
// call some other method that use FileInfo, and they will also use
// MyNullFileInfo instead
}
 
Aquila said:
Hi all!

I wondering if there is any method to override object/class within a
context, like this:
<snip>

If I understand correctly, you're asking if you can override this kind
of statement:

SomeType x = new SomeType();

to actually produce an SomeOtherType object instead, and the answer is
yes, you do it like this:

SomeType x = new SomeOtherType();

of course, this means SomeOtherType must descend from SomeType.

Now, if I understand the full question correctly, you're asking if you
can ask methods you call from your code do the same substitution
automatically, ie. every place they do the first statement they should
do the second statement instead, then the answer is no.
 
Lasse said:
<snip>

If I understand correctly, you're asking if you can override this kind
of statement:

SomeType x = new SomeType();

to actually produce an SomeOtherType object instead, and the answer is
yes, you do it like this:

SomeType x = new SomeOtherType();

of course, this means SomeOtherType must descend from SomeType.

Now, if I understand the full question correctly, you're asking if you
can ask methods you call from your code do the same substitution
automatically, ie. every place they do the first statement they should
do the second statement instead, then the answer is no.

What I want is not just substution, but some kind of dynamic-scoping
support. I'm thinking of using global delegates (and static methods to
create obj) which can be easily substituted:

DelegateTemplate<SomeType> CreateSomeType = delegate() { return new
SomeType(); }

But it would need thread/fiber-local variables (is there such thing in
..net?) and a mechanism to ensure the substitution is always restored
after the context (maybe try-finally can work).
 
Back
Top