C# 泛型方法并将任何枚举作为参数传递
全部, 我正在尝试在枚举框架中整理我的最后一部分 我的目标是:发送任何枚举类型并将其转换为列表,然后将其绑定到下拉列表。我将使用ObjectDataSource作为给定下拉列表的数据源。我想创建一个只接受一个参数的复合控件;枚举类型。复合控件将整理数据绑定和所有其他位和BOP 现在,我唯一的问题是将泛型方法转换为与ObjectDataSource兼容 下面是我当前方法的代码,我需要在ObjectDataSource上使用它。因此,此方法工作正常,并返回枚举类型WeekDays的项列表。但是,我需要相同的功能,但是我需要用任何类型的枚举替换工作日 代码:C# 泛型方法并将任何枚举作为参数传递,c#,.net,generics,enums,objectdatasource,C#,.net,Generics,Enums,Objectdatasource,全部, 我正在尝试在枚举框架中整理我的最后一部分 我的目标是:发送任何枚举类型并将其转换为列表,然后将其绑定到下拉列表。我将使用ObjectDataSource作为给定下拉列表的数据源。我想创建一个只接受一个参数的复合控件;枚举类型。复合控件将整理数据绑定和所有其他位和BOP 现在,我唯一的问题是将泛型方法转换为与ObjectDataSource兼容 下面是我当前方法的代码,我需要在ObjectDataSource上使用它。因此,此方法工作正常,并返回枚举类型WeekDays的项列表。但是,我需
公共类下拉数据
{
公共EnumDataItemList GetList()
{
EnumDataItemList items=新的EnumDataItemList();
foreach(Enum.GetValues中的int值(工作日))
{
EnumDataItem=新的EnumDataItem();
工作日d=(工作日)值;
//设置显示文本
如果(!string.IsNullOrEmpty(DataHandlers.GetAttributeValue(d)))
{
//翻译逻辑就在这里
item.Text=DataHandlers.GetAttributeValue(d);
}
其他的
{
//翻译逻辑就在这里
item.Text=Enum.GetName(typeof(平日),值);
}
item.Value=Value;//实际值
item.ToolTip=DataHandlers.GetAttributeValue(d);
item.Description=DataHandlers.GetAttributeValue(d);
item.HelpText=DataHandlers.GetAttributeValue(d);
item.ExcludeOnBinding=DataHandlers.GetAttributeValue(d);
如果(!item.ExcludeOnBinding)
{
项目。添加(项目);
}
}
退货项目;
}
}
公共类EnumDataItemList:列表
{
}
据我所知,我不能对ObjectDataSOurce使用泛型方法,但泛型类可以。我只是不能让它与普通类一起工作,非常感谢所有的帮助。当一切顺利时,我很乐意与大家分享完整的解决方案
我正在使用Framework2.0。这应该会对您有所帮助。(如果T不是枚举类型,则将引发异常。)
这应该对你有帮助。(如果T不是枚举类型,则将引发异常。)
Magnus’的替代方案,但想法几乎完全相同(只是在被击败后不想扔掉它;-)-只是迭代枚举值而不是int。相同用法:
public static class DropDownData
{
// struct, IComparable, IFormattable, IConvertible is as close as we'll
// get to an Enum constraint. We don't actually use the constraint for
// anything except rudimentary compile-time type checking, though, so
// you may leave them out.
public static EnumDataItemList GetList<T>()
where T : struct, IComparable, IFormattable, IConvertible
{
// Just to make the intent explicit. Enum.GetValues will do the
// type check, if this is left out:
if (!typeof(T).IsEnum)
{
throw new ArgumentException("Type must be an enumeration");
}
EnumDataItemList items = new EnumDataItemList();
foreach (Enum e in Enum.GetValues(typeof(T)))
{
EnumDataItem items = new EnumDataItem();
// Note: This assumes the enum's underlying type is
// assignable to Int32 (for example, not a long):
int value = Convert.ToInt32(e);
// The same attribute retrieval code as in the
// WeekDays example, including:
item.Text = e.ToString(); // e is Enum here, no need for GetName
}
}
}
公共静态类下拉数据
{
//struct、IComparable、IFormattable、IConvertible与我们所说的尽可能接近
//获取枚举约束。我们实际上不使用枚举约束
//但是,除了基本的编译时类型检查之外,其他任何东西都可以
//你可以把它们删掉。
公共静态EnumDataItemList GetList()
其中T:struct,i可比较,i可附加,i可转换
{
//只是为了明确目的,Enum.GetValues将执行以下操作:
//类型检查,如果忽略此项:
if(!typeof(T).IsEnum)
{
抛出新ArgumentException(“类型必须是枚举”);
}
EnumDataItemList items=新的EnumDataItemList();
foreach(Enum.GetValues中的Enum e(typeof(T)))
{
EnumDataItems=新的EnumDataItem();
//注意:这假定枚举的基础类型为
//可分配给Int32(例如,不长):
int值=转换为32(e);
//与中相同的属性检索代码
//工作日示例,包括:
item.Text=e.ToString();//这里e是枚举,不需要GetName
}
}
}
Magnus的替代方案,但想法几乎完全相同(只是在被击败后不想扔掉它;-)-只是迭代枚举值而不是int。相同用法:
public static class DropDownData
{
// struct, IComparable, IFormattable, IConvertible is as close as we'll
// get to an Enum constraint. We don't actually use the constraint for
// anything except rudimentary compile-time type checking, though, so
// you may leave them out.
public static EnumDataItemList GetList<T>()
where T : struct, IComparable, IFormattable, IConvertible
{
// Just to make the intent explicit. Enum.GetValues will do the
// type check, if this is left out:
if (!typeof(T).IsEnum)
{
throw new ArgumentException("Type must be an enumeration");
}
EnumDataItemList items = new EnumDataItemList();
foreach (Enum e in Enum.GetValues(typeof(T)))
{
EnumDataItem items = new EnumDataItem();
// Note: This assumes the enum's underlying type is
// assignable to Int32 (for example, not a long):
int value = Convert.ToInt32(e);
// The same attribute retrieval code as in the
// WeekDays example, including:
item.Text = e.ToString(); // e is Enum here, no need for GetName
}
}
}
公共静态类下拉数据
{
//struct、IComparable、IFormattable、IConvertible与我们所说的尽可能接近
//获取枚举约束。我们实际上不使用枚举约束
//但是,除了基本的编译时类型检查之外,其他任何东西都可以
//你可以把它们删掉。
公共静态EnumDataItemList GetList()
其中T:struct,i可比较,i可附加,i可转换
{
//只是为了明确目的,Enum.GetValues将执行以下操作:
//类型检查,如果忽略此项:
if(!typeof(T).IsEnum)
{
抛出新ArgumentException(“类型必须是枚举”);
}
EnumDataItemList items=新的EnumDataItemList();
foreach(Enum.GetValues中的Enum e(typeof(T)))
{
EnumDataItems=新的EnumDataItem();
//注意:这假定枚举的基础类型为
//可分配给Int32(例如,不长):
int值=转换为32(e);
//与中相同的属性检索代码
//工作日示例,包括:
item.Text=e.ToString();//这里e是枚举,不需要GetName
}
}
}
我对这个问题的处理方式似乎有点不同,我想出了一些非常简洁的方法,似乎对我有效。我不能声称这是原创作品,因为我不记得我是否找到了它并复制了它,或者我是否将它与我自己的一些原创作品结合在一起。我在枚举上放置了一个扩展方法,该方法为我提供了一个ToDisplayText函数,用于获取相同名称的自定义枚举属性
this.ddlBlah.DataSource =
Enum.GetValues(typeof(MyEnum)).Cast<MyEnum>()
.ToDictionary(x => x, x => x.ToDisplayText());
this.ddlBlahs.DataValueField = "key";
this.ddlBlah.DataTextField = "value";
this.ddlBlah.DataBind();
public static string ToDisplayText(this Enum Value)
{
try
{
Type type = Value.GetType();
MemberInfo[] memInfo = type.GetMember(Value.ToString());
if (memInfo != null && memInfo.Length > 0)
{
object[] attrs = memInfo[0].GetCustomAttributes(
typeof(DisplayText),
false);
if (attrs != null && attrs.Length > 0)
return ((DisplayText)attrs[0]).DisplayedText;
}
}
catch (Exception ex)
{
throw new Exception("Your favorite error handling here");
}
return Value.ToString();
// End of ToDisplayText()
}
[System.AttributeUsage(System.AttributeTargets.Field)]
public class DisplayText : System.Attribute
{
public string DisplayedText;
public DisplayText(string displayText)
{
DisplayedText = displayText;
}
// End of DisplayText class definition
}
this.ddlBlah.DataSource=
Enum.GetValues(typeof(MyEnum)).Cast()的类型
.ToDictionary(x=>x,x=>x.ToDisplayText());
this.ddlBlahs.DataValueField=“key”;
this.ddlBlah.DataTextField=“value”
public static EnumDataItemList GetList(Type t)
{
EnumDataItemList items = new EnumDataItemList();
foreach (int e in Enum.GetValues(t))
{
EnumDataItem item = new EnumDataItem();
item.Text = Enum.GetName(t, e);
item.Value = e;
}
//Rest of code goes here
}
public static class DropDownData
{
// struct, IComparable, IFormattable, IConvertible is as close as we'll
// get to an Enum constraint. We don't actually use the constraint for
// anything except rudimentary compile-time type checking, though, so
// you may leave them out.
public static EnumDataItemList GetList<T>()
where T : struct, IComparable, IFormattable, IConvertible
{
// Just to make the intent explicit. Enum.GetValues will do the
// type check, if this is left out:
if (!typeof(T).IsEnum)
{
throw new ArgumentException("Type must be an enumeration");
}
EnumDataItemList items = new EnumDataItemList();
foreach (Enum e in Enum.GetValues(typeof(T)))
{
EnumDataItem items = new EnumDataItem();
// Note: This assumes the enum's underlying type is
// assignable to Int32 (for example, not a long):
int value = Convert.ToInt32(e);
// The same attribute retrieval code as in the
// WeekDays example, including:
item.Text = e.ToString(); // e is Enum here, no need for GetName
}
}
}
this.ddlBlah.DataSource =
Enum.GetValues(typeof(MyEnum)).Cast<MyEnum>()
.ToDictionary(x => x, x => x.ToDisplayText());
this.ddlBlahs.DataValueField = "key";
this.ddlBlah.DataTextField = "value";
this.ddlBlah.DataBind();
public static string ToDisplayText(this Enum Value)
{
try
{
Type type = Value.GetType();
MemberInfo[] memInfo = type.GetMember(Value.ToString());
if (memInfo != null && memInfo.Length > 0)
{
object[] attrs = memInfo[0].GetCustomAttributes(
typeof(DisplayText),
false);
if (attrs != null && attrs.Length > 0)
return ((DisplayText)attrs[0]).DisplayedText;
}
}
catch (Exception ex)
{
throw new Exception("Your favorite error handling here");
}
return Value.ToString();
// End of ToDisplayText()
}
[System.AttributeUsage(System.AttributeTargets.Field)]
public class DisplayText : System.Attribute
{
public string DisplayedText;
public DisplayText(string displayText)
{
DisplayedText = displayText;
}
// End of DisplayText class definition
}