المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Generic question



C# Programming
08-10-2009, 08:00 PM
Suppose I have a generic class A with a method Xyz.

public class A : where T : class
{
// lots of stuff omitted...

public int Xyz()
{ // details omitted
}
}

Elsewhere I have an object reference. I want to test if the reference refers to an instance of A (don't care about what is), and if it is an instance of A, invoke method Xyz and get the return value. Something like

if (ref is A)
{
int returnValue = ((A)ref).Xyz();
}

except that this code doesn't work when A is a generic type. Can this be done?

Thanks

Peter