Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.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# 如何使用枚举的System.Type从枚举创建IEnumerable_C#_Reflection_Enums - Fatal编程技术网

C# 如何使用枚举的System.Type从枚举创建IEnumerable

C# 如何使用枚举的System.Type从枚举创建IEnumerable,c#,reflection,enums,C#,Reflection,Enums,我正在尝试取消枚举类型序列化 我需要这个原型的方法: private static object CSVConvertParam(string value, System.Type t); 所以我可以这样使用它: enum MyEnum { val1=0, val2=1, val3=2 ...} ... System.Type enumType = typeof(MyEnum); ... var unserializedVal = CSVConvertParam("val3", enumTyp

我正在尝试取消枚举类型序列化

我需要这个原型的方法:

private static object CSVConvertParam(string value, System.Type t);
所以我可以这样使用它:

enum MyEnum { val1=0, val2=1, val3=2 ...}
...
System.Type enumType = typeof(MyEnum);
...
var unserializedVal = CSVConvertParam("val3", enumType );
我读过一个几乎相似的问题:

但在我的例子中,类型在编译时是未知的

它必须非常接近这一点:

private static object CSVConvertParam(string value, System.Type t)
{
 int enumIndex = ( (IEnumerable<????>) t.GetEnumValues()).ToList().IndexOf( value);
return  (Enum)Enum.ToObject(t, enumIndex);

}
私有静态对象CSVConvertParam(字符串值,System.Type t)
{
int enumIndex=((IEnumerable)t.GetEnumValues()).ToList().IndexOf(value);
返回(Enum)Enum.ToObject(t,enumIndex);
}
除了我需要知道t表示的具体类型,才能使(IEnumerable)转换工作

有办法解决这个问题吗

编辑: 我试图制作一个通用版本来解决这个问题:

 private static object CSVConvertParam<T>(string value)
 {
    if (typeof(T).IsEnum)
      {
        int enumIndex = ((IEnumerable<T>) typeof(T).GetEnumValues()).ToList().IndexOf(value); // this actually does not work and needs to be worked on 
             return  (Enum)Enum.ToObject(t, enumIndex);
       }
}
私有静态对象CSVConvertParam(字符串值)
{
if(类型(T).IsEnum)
{
int enumIndex=((IEnumerable)typeof(T).GetEnumValues()).ToList().IndexOf(value);//这实际上不起作用,需要处理
返回(Enum)Enum.ToObject(t,enumIndex);
}
}
但是假设我设法使这个方法正确工作,那么编译器似乎不允许我调用它,因为我的意思是:

 string[] propertiesNames = ...;
  PropertyInfo propertyInfo = properties.FirstOrDefault(p => p.Name.Equals(propertiesNames[i]));
  paramValue = CSVConvertParam<propertyInfo.PropertyType>(objectPropertiesValues[i]);
string[]属性名称=。。。;
PropertyInfo PropertyInfo=properties.FirstOrDefault(p=>p.Name.Equals(propertiesNames[i]);
paramValue=CSVConvertParam(objectPropertiesValues[i]);
编译器不接受CSVConvertParam: “找不到类型或命名空间propertyInfo…” 我再次假设propertyInfo.PropertyType是一个System.Type,而需要一个具体的类型。

请尝试
Enum.GetNames(Type)
。这将返回一个
字符串[]
,然后您可以在该数组中找到字符串的索引

但是,正如其他人所说,您可能只是重新发明了
Enum.Parse(Type,string)
尝试
Enum.GetNames(Type)
。这将返回一个
字符串[]
,然后您可以在该数组中找到字符串的索引


但是,正如其他人所说,您可能只是在重新发明
Enum.Parse(Type,string)

为什么不使用
Enum.Parse
Enum.TryParse
?同意。此方法的签名看起来与相同,只是参数颠倒了。因为Enum.TryParse需要我知道具体类型。TryParse(值,输出结果)将起作用。但我不知道编译时的类型是什么。我只有typeof(MyEnum)。我不知道如何从包含typeof(T)的方法参数切换回类型T。为什么不使用
Enum.Parse
Enum.TryParse
?同意。此方法的签名看起来与相同,只是参数颠倒了。因为Enum.TryParse需要我知道具体类型。TryParse(值,输出结果)将起作用。但我不知道编译时的类型是什么。我只有typeof(MyEnum)。我不知道如何从包含typeof(T)的方法参数切换回类型T。