如果我有一个被限制为类型'int'的泛型方法,那么我当然应该能够将一个整数转换为泛型T类型.例如...
public T ExampleMethod<T>(int unchanged) where T : int
{
return (T)unchanged;
}
Run Code Online (Sandbox Code Playgroud)
...编译器抱怨无法将类型'int'转换为'T',但我有一个约束,表明目标是整数.那肯定应该有用吗?
更新:
实际情况是我想要一个返回枚举值的辅助方法.所以我理想的助手方法会更像这样....
public T GetAttributeAsEnum<T>(XmlReader reader, string name) where T : enum
{
string s = reader.GetAttribute(name);
int i = int.Parse(s);
return (T)i;
}
Run Code Online (Sandbox Code Playgroud)
......并像这样使用它......
StateEnum x = GetAttributeAsEnum<StateEnum>(xmlReader, "State");
CategoryEnum y = GetAttributeAsEnum<CategoryEnum>(xmlReader, "Category");
OtherEnum z = GetAttributeAsEnum<OtherEnum>(xmlReader, "Other");
Run Code Online (Sandbox Code Playgroud)
......但你无法通过枚举来约束.
我们知道,如果我们为基类型定义模板,那么该模板也可以用于派生类型(如果没有使用任何其他模板来覆盖它).
因为我们不能继承一个Enum,也不会enum被认为是继承的Enum,所以对于对象的不同自定义枚举属性,它们中的Enum.cshtml模板Views\Shared\EditorTemplates都不会处于活动状态,如下所示:
public enum Role
{
Admin,
User,
Guest
}
Run Code Online (Sandbox Code Playgroud)
我已经在ASP中看到了关于这个主题的一些答案,但是我想知道在MVC 4中是否对这个主题有一些改进?
PS.我的意思是不使用任何明确的模板归属(如@Html.EditorFor(model => model.Role, "Enum")或[UIHint("Enum")])
PPS.我是MVC的新手,所以我很感激你的简单答案.
我有一个方法,我用来将字符串转换为通用枚举.可以将结果值分配给可以为空的字段.
对于不可为空的情况,以下工作正常,但我无法弄清楚如何正确调整它以允许可为空的返回类型,比如在参数为null的情况下,或者实际上如何允许返回类型可能是null或不可为空(也许这是不可能的?).每置换Nullable<T>和T?,我试过仍然导致的返回值和方法名称都被红色squiggleys
private static T ConvertStringToEnumValue<T>(string value)
{
// Converts string to a given enum-type T value.
T EnumValue;
try
{
EnumValue = (T)Enum.Parse(typeof(T), value);
if (!Enum.IsDefined(typeof(T), EnumValue))
{
return default(T); // 0 for enum
}
}
catch (Exception e)
{
return default(T); // 0 for enum
}
return EnumValue;
}
Run Code Online (Sandbox Code Playgroud) 我需要我的枚举返回一个特定的字符串,但我无法弄清楚如何让它返回一个包含中断的字符串而无需进行转换的方法.没有辅助方法可以LicenseTypes.DISCOUNT_EARLY_ADOPTER返回DISCOUNT EARLY-ADOPTER吗?
// All license types
public enum LicenseTypes
{
DISCOUNT,
DISCOUNT_EARLY_ADOPTER,
COMMERCIAL,
COMMERCIAL_EARLY_ADOPTER
}
// Convert enum to correct string
public static string LicenseTypeToString(LicenseTypes Enum)
{
if (Enum == LicenseTypes.COMMERCIAL)
return "COMMERCIAL";
else if (Enum == LicenseTypes.COMMERCIAL_EARLY_ADOPTER)
return "COMMERCIAL EARLY-ADOPTER";
else if (Enum == LicenseTypes.DISCOUNT)
return "DISCOUNT";
else if (Enum == LicenseTypes.DISCOUNT_EARLY_ADOPTER)
return "DISCOUNT EARLY-ADOPTER";
else
return "ERROR";
}
Run Code Online (Sandbox Code Playgroud) 我想我可能在做一些愚蠢的事情,但我正在尝试编写一个通用函数,该函数接受 astring并将其转换为 an enum(然后做了一些我为了简洁而跳过的其他东西)。问题是,它抱怨Enum.TryParse需要一个不可为空的类型,它抱怨 T 是可以为空的;看似可以System.Enum为空,但实际枚举不可为空。是我在这里做错了什么还是有办法解决这个问题。
private T GetEnumFilter<T>(string strValue) where T : Enum
{
return Enum.TryParse(strValue, out T value) ? value : throw new Exception("Invalid value");
}
Run Code Online (Sandbox Code Playgroud)
我看过这个/sf/answers/566075191/答案和dotnet 样本中的样本,但看不出我做错了什么。
试图模仿这篇文章:创建将T限制为枚举的通用方法
public static string[] ToTextArray(this Dictionary<string, T> dictionary) where T:struct, IConvertible
{
...
}
Run Code Online (Sandbox Code Playgroud)
看来我T无法解决.为所有Dictionary<string, enum_type>类编写扩展方法的正确方法是什么?