Dynamic casting
Why do the two last lines not work and give me
The type or namespace name 'myType' could not be found
Type myType = this.GetType();
bool test = obj 开发者_开发技巧is myType;
var p = (myType)obj;
You need to do:
bool test = myType.IsInstanceOfType(obj);
or
bool test = myType.IsAssignableFrom(obj.GetType());
// var p = Convert.ChangeType(obj, myType); - update: this is not what the OP asked
For the second one, you can't "cast" an expression to a type which is not known at compile time. The point of casting is to reference the members of that type natively. If you don't know what the type is at compile type (because you are using .GetType()
), then there is no point casting, and indeed it's impossible.
C# is a statically typed language, which means types must be known at compile time. The var
keyword just means "Figure out what this type should be automatically (through inference) at compile time".
Any cast or type-check has to be against a static type. You are actually trying to use an instance of an object of type Type
that describes a type. That instance is provided to you by .NET Reflection.
Once you are working with an object of a type derived at runtime you have to use reflection for all operations on that instance. For example, you can do something like this:
bool test = myType.IsInstanceOfType(obj);
bool test = typeof(obj).IsAssignableFrom(myType); // Good for checking if a type implements an interface
For your second line, you can use an object
reference to hold any type:
object p = obj;
The type of object being cast to must be determined at compile time in C#. You're trying to make a cast based on the runtime type of the object which is incompatible with this notion. Hence you get this error.
Can you give us an example of what you want to do with p
? There is likely a cleaner way.
If you want to know if an object is an instance or implementation of a Type
at runtime, you need to do:
Type thisType = this.GetType();
Type objType = obj.GetType();
if(objType.IsAssignableFrom(type))
{
// do your stuff
}
From there, there is no way to cast an object to a runtime type since the compiler need that information at compile time. However, if you are using C# 4, you can use the dynamic
type.
Type thisType = this.GetType();
Type objType = obj.GetType();
if(objType.IsAssignableFrom(type))
{
dynamic dynObj = obj;
dynObj.CallWhateverIWant();
}
However, looking at your code, there is obviously a better way to do want you want to do. Maybe you could implement some kind of interface, common to both classes and use functions from that interface at compile time.
I don't have a Visual Studio ready at the moment and this isn't something I need to do often, but for the second line I think it should look like this:
bool test = obj is typeof(this);
The third line is not possible unless you have a limited set of possible types you can switch on.
the is operator works on classes, not objects of type Type
If you want to do this sort of thing, use the dynamic keyword
http://msdn.microsoft.com/en-us/library/dd264736.aspx
精彩评论