C# 使用类型变量强制转换对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9005427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
cast object with a Type variable
提问by Michael Schnerring
The following doesn't work, of course. Is there a possible way, which is pretty similar like this?
当然,以下不起作用。有没有可能的方法,这非常相似?
Type newObjectType = typeof(MyClass);
var newObject = givenObject as newObjectType;
采纳答案by Aliostad
newObjectTypeis an instanceof the Typeclass (containing metadata about the type) not the typeitself.
newObjectType是一个实例的的Type类(关于型含元数据)不是类型本身。
This should work
这应该工作
var newObject = givenObject as MyClass;
OR
或者
var newObject = (MyClass) givenObject;
Casting to an instance of a type really does not make sense since compile timehas to know what the variable type should be while instance of a type is a runtimeconcept.
强制转换为类型的实例确实没有意义,因为编译时必须知道变量类型应该是什么,而类型的实例是运行时概念。
The only way varcan work is that the type of the variable is known at compile-time.
唯一var可行的方法是在编译时知道变量的类型。
UPDATE
更新
Casting generally is a compile-time concept, i.e. you have to know the type at compile-time.
强制转换通常是一个编译时概念,即您必须在编译时知道类型。
Type Conversion is a runtime concept.
类型转换是一个运行时概念。
UPDATE 2
更新 2
If you need to make a call using a variable of the type and you do not know the type at compile time, you can use reflection: use Invokemethod of the MethodInfoon the type instance.
如果需要使用该类型的变量进行调用,并且在编译时不知道该类型,则可以使用该类型实例上的反射:useInvoke方法MethodInfo。
object myString = "Ali";
Type type = myString.GetType();
MethodInfo methodInfo = type.GetMethods().Where(m=>m.Name == "ToUpper").First();
object invoked = methodInfo.Invoke(myString, null);
Console.WriteLine(invoked);
Console.ReadLine();
回答by StuartLC
You can check if the type is present with IsAssignableFrom
您可以使用 IsAssignableFrom 检查该类型是否存在
if(givenObject.GetType().IsAssignableFrom(newObjectType))
But you can't use var here because type isn't known at compile time.
但是你不能在这里使用 var 因为类型在编译时是未知的。
回答by Tomislav Markovski
Maybe you can solve this using generics.
也许你可以使用泛型来解决这个问题。
public void CastToMyType<T>(object givenObject) where T : class
{
var newObject = givenObject as T;
}
回答by Silas
I recently had the case, that I needed to generatesome code like in Tomislav's answer. Unfortunately during generation time the type T was unknown. However, a variable containing an instance of that type was known. A solutiondirty hack/ workaround for that problem would be:
我最近遇到了这样的情况,我需要像Tomislav's answer那样生成一些代码。不幸的是,在世代时期,T 型是未知的。但是,包含该类型实例的变量是已知的。该问题的解决方案脏黑客/解决方法是:
public void CastToMyType<T>(T hackToInferNeededType, object givenObject) where T : class
{
var newObject = givenObject as T;
}
Then this can be called by CastToMyType(instanceOfNeededType, givenObject)and let the compiler infer T.
然后可以调用它CastToMyType(instanceOfNeededType, givenObject)并让编译器推断 T。
回答by Sebastian Badea
You can use Convert.ChangeType. According to msdn, it
您可以使用 Convert.ChangeType。根据msdn,它
returns an object of a specified type whose value is equivalent to a specified object.
返回一个指定类型的对象,其值等同于指定的对象。
You could try the code below:
你可以试试下面的代码:
Type newObjectType = typeof(MyClass);
var newObject = Convert.ChangeType(givenObject, newObjectType);

