Shneeg Shneeg - 3 months ago 11
C# Question

Interface casting

I have a few classes i can't change. They have one property

Prop3
in common:

public class c1
{
public string Prop1 { get; set; }
public string Prop2 { get; set; }
public string Prop3 { get; set; }
}

public class c2
{
public string Prop2 { get; set; }
public string Prop3 { get; set; }
}

public class c3
{
public string Prop5 { get; set; }
public string Prop3 { get; set; }
}


Now I want to acces this property without knowing the type. I thought of using a Interface:

public interface ic
{
string Prop3 { get; set; }
}


But this code throws a invalid cast exception:

c1 c1o = new c1() { Prop3 = "test" };
string res = ((ic)c1o).Prop3;

Jcl Jcl
Answer

C# doesn't support compile-time duck-typing, so if you can't change your types, no luck on that.

You can access your property using dynamic, which allows runtime duck-typing (no compile time checking though, and you lose intellisense if you use Visual Studio):

c1 c1o = new c1() { Prop3 = "test" };
string res = ((dynamic)c1o).Prop3;

Or via reflection:

c1 c1o = new c1() { Prop3 = "test" };
string res = (string)c1o.GetType().GetProperty("Prop3").GetValue(c1o);

Since there's no compile-time checking, you'll need to handle exceptions in case you pass an instance with no Prop3.

Or if the types are not sealed, you can try implementing your own derived types where you can specify an interface:

public interface ic
{
   string Prop3 { get; set; }
}

public class c1d : c1, ic {}
public class c2d : c2, ic {}
public class c3d : c3, ic {}

This would require you control the creation of the instances though, instances will need to be of type c1d, c2d, c3d, won't work if you get objects of type c1, c2 or c3

You can do explicit type conversions as @David pointed out (which is a clever trick), but that means you'll have two instances of your object. For a very simple case like the one presented in the question, it might do... if you need anything more advanced, that might be quite tricky