I have a class:
public class MyClass<T>
{
public string TestProperty { get; set; }
}
and I want to create a delegate to run on instances of this class, such as:
Action<MyClass<object>> myDelegate = myclass => myclass.TestProperty = "hello";
However, the above delegate can't be invoked with anything other than a MyClass<object>
, such as MyClass<DateTime>
or MyClass<string>
.
How can I either define the delegate, or modify the delegate so that I can execute the delegate on a MyClass<T>
where T
is anything which extends object
?
Edit: This can wait until C# 4 if thats when this becomes possible (if so, please still tell me how) although i'd prefer to get on with it now in 3.5
Edit: I actually also have a 2nd class:
public class MyDerivedClass<T1, T2> : MyClass<T1>
{
public int OtherProp { get; set; }
}
Ideally id like to use the following syntax to define some delegates:
CreateDelegate<MyClass<object>>(mc => mc.TestProperty = "hello");
CreateDelegate<MyDerivedClass<object, object>>(mc => mc.OtherProp = 4);
Then given an object, id like to see which delegate arguments match, and then run them
Is this possible? What alternatives do I have to create such delegates?
Thanks