Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从类型名的字符串表示形式转换为类型_C#_.net_Reflection_System.reflection - Fatal编程技术网

C# 从类型名的字符串表示形式转换为类型

C# 从类型名的字符串表示形式转换为类型,c#,.net,reflection,system.reflection,C#,.net,Reflection,System.reflection,我该怎么做?假设这是C#2.0,那么赋值表达式左侧应该是什么。我没有var关键字。不幸的是,在.NET中无法实现您想要的功能 可能的部分解决方案包括: 如果您在编译时知道该类型(不太可能,因为您是在运行时从字符串创建的),则只需将其转换为该类型: sTypeName = ... //do some string stuff here to get the name of the type /* The Assembly.CreateInstance function returns a typ

我该怎么做?假设这是C#2.0,那么赋值表达式左侧应该是什么。我没有var关键字。

不幸的是,在.NET中无法实现您想要的功能

可能的部分解决方案包括:

  • 如果您在编译时知道该类型(不太可能,因为您是在运行时从字符串创建的),则只需将其转换为该类型:

    sTypeName = ... //do some string stuff here to get the name of the type
    
    /*
    The Assembly.CreateInstance function returns a type
    of System.object. I want to type cast it to 
    the type whose name is sTypeName.
    
    assembly.CreateInstance(sTypeName)
    
    So, in effect I want to do something like:
    
    */
    
    assembly.CreateInstance(sTypeName) as Type.GetType(sTypeName);
    
  • 如果您知道所有可能的类型都将实现一个特定的通用接口,那么您可以转换到该接口:

    YourType t = (YourType)Activator.CreateInstance(sTypeName);
    

  • 如果你不能做到以上任何一点,那么很不幸,你只能使用
    对象
    和反射。

    通常你让所有类,你想动态地实例化它,实现一个公共接口,比如说
    IMyInterface
    。您可以从类名字符串创建一个实例,如下所示:

    IYourInterface i = (IYourInterface)Activator.CreateInstance(sTypeName);
    
    namespace MyNamespace
    {
        public class MyClass
        {
            public MyClass(string s, int i) { }
    
            public int TwoTimes(int i)
            {
                return i * 2;
            }
        }
    }
    
    即使您没有公共接口,但知道方法的名称(作为字符串),也可以像这样调用方法(对于属性、事件等非常类似):

    示例类:

    object instance = Activator.CreateInstance(classtype);
    
    int result = (int)classtype.GetMethod("TwoTimes").Invoke(instance, 
                            new object[] { 15 });
    // result = 30
    

    在类中定义泛型方法,然后可以按如下方式强制转换:

    IYourInterface i = (IYourInterface)Activator.CreateInstance(sTypeName);
    
    namespace MyNamespace
    {
        public class MyClass
        {
            public MyClass(string s, int i) { }
    
            public int TwoTimes(int i)
            {
                return i * 2;
            }
        }
    }
    
    publictcast(objectobj)
    {
    返回(T)obj;
    }
    字符串sTypename=“SomeClassName”;
    MethodInfo cast=this.GetType().GetMethod(“cast”);
    MethodInfo genericCast=cast.MakeGenericMethod(新类型[]{Type.GetType(sTypename)});
    Object castedValue=genericCast.Invoke(这是一个新对象[]{instanceToBeCasted});
    
    但是我想,如果您不能将强制转换的值存储在实际类型的变量中,那么这种强制转换的意义何在,因为您在编写代码时不知道实际类型