It would return the actual type ClassA
.
All the type information is available and inspectable via reflection. You simply can't directly reference the type at compile-time.
Project 2 could still call members on ClassA
, it would just be difficult/cumbersome because you would be limited to using reflection or dynamic
.
public void MyMethod() {
Type type = MyItem.GetType(); //gets a `System.Type` representing `ClassA'
Console.WriteLine(type.FullName);//outputs "Project1.ClassA"
}
Just do demonstrate what you can or cannot do, say ClassA
was defined as:
public class ClassA : IMyInterface
{
public string MyField = "Hello world!";
}
You would not be able to do this:
public void MyMethod() {
Console.WriteLine(MyItem.MyField); //compiler error
}
You could do this because Project2 can access information from Project1 at runtime:
public void MyMethod() {
//lookup the field via reflection
Type type = MyItem.GetType();
Console.WriteLine(type.GetField("MyField").GetValue(MyItem));
//simpler way than above using dynamic, but still at runtime
dynamic dynamicItem = MyItem;
Console.WriteLine(MyItem.MyField);
}
But you couldn't do this because Project2 has no knowledge of Project1 at compile time:
public void MyMethod() {
//cast to type ClassA
ClassA classAMyItem = (ClassA)MyItem; //compile error
Console.WriteLine(classAMyItem.MyField); //compile error
}
This is basically one of the tenants of polymorphism though. Your MyMethod
shouldn't know, and shouldn't care what class MyItem
is beyond it being an IMyInterface
. It should generally only care about accessing the properties, methods, and events as defined on IMyInterface
. If it does care about it being an instance of ClassA
, then you might want to reconsider your design or usage.