tags:

views:

196

answers:

4

Is it possible to allow methods and properties of the 'this' pointer to be resolved dynamically?

Put another way, can a class have a dynamic superclass?

Clarification

I would like to be able to subclass some class and access properties and methods that aren't defined at compile-time.

class MyClass : DynamicObject
{
    public void ReceiveValue(object value) {
        MyProperty = value;
    }
}

DynamicObject provides a way for my code to get notified that set_MyProperty has been called with the argument value above, correct? I know this is possible if you use a syntax like:

var mc = new MyClass();
...
dynamic dmc = mc;
dmc.MyProperty = value;

But I want to be able to do this from within the methods of MyClass, almost as if I had done:

dynamic dmc = this;
dmc.MyProperty = value;

Does DynamicObject have me covered?

A: 

This is the basis of polymorphism. The method/property called will be the one given lowest in the heirarchy of the objects type.

tster
I mean resolving the property or method at runtime via C# 4.0 dynamic call site binding.
bvanderveen
If I understand bvanderveen correctly he is asking about doing the opposite of polymorphism or at least in the other direction.
Jonas Elfström
+3  A: 

No, you can't have a dynamic base class. Aside from anything else, the system still needs to know how much space to allocate when you create a new instance of your class.

Could you explain what you're trying to achieve? There may well be ways in which dynamic would help without needing quite this behaviour.

EDIT: Okay, having seen your edit - I don't think you can quite do what you want, but if you just use the

dynamic dmc = this;
dmc.MyProperty = value;

or

((dynamic)this).MyProperty = value;

workaround it should be fine. To put it another way: the this reference is always statically typed, but you can have an expression with the value of this but with a dynamic type.

That shouldn't be too onerous unless you're doing a lot of dynamic work - in which case I'd recommend that you use a fully dynamic language instead. If you implement the bulk of your dynamic code in IronPython/IronRuby, you can easily integrate it with your C# code anyway.

Jon Skeet
Clarified. Thanks.
bvanderveen
Thanks. Doing something fully dynamic is an interesting idea; unfortunately last time I tested the DLR (v0.91, specifically the hosting API if I recall correctly) leaked memory, and my application is a long-running process.
bvanderveen
I suspect that things are a lot further on now than they used to be :) Note that if you use `dynamic` *at all* in your C# code, you're already using the DLR.
Jon Skeet
A: 

How about this:

    class B
 {
  public void M(object o)
  {
   dynamic i = this;
   i.P = o;
  }
 }

 class D : B
 {
  public object P { get; set; }
 }

 class Program
 {
  static void Main()
  {
   var d = new D();
   d.M(1);
  }
 }
Dzmitry Huba
A: 

I realize this is a tangent, but there are languages where every class's superclass is dynamic - i.e. where class name resolution is virtual and override-able.

Eamon Nerbonne