在c#中,是否有可能用属性装饰Enum类型或做一些其他事情来指定默认值应该是什么,而不改变值?由于某种原因,所需的数字可能是固定不变的,而且仍然可以控制默认值是很方便的。
enum Orientation
{
None = -1,
North = 0,
East = 1,
South = 2,
West = 3
}
Orientation o; // Is 'North' by default.
在c#中,是否有可能用属性装饰Enum类型或做一些其他事情来指定默认值应该是什么,而不改变值?由于某种原因,所需的数字可能是固定不变的,而且仍然可以控制默认值是很方便的。
enum Orientation
{
None = -1,
North = 0,
East = 1,
South = 2,
West = 3
}
Orientation o; // Is 'North' by default.
当前回答
[DefaultValue(None)]
public enum Orientation
{
None = -1,
North = 0,
East = 1,
South = 2,
West = 3
}
然后在代码中可以使用
public Orientation GetDefaultOrientation()
{
return default(Orientation);
}
其他回答
[DefaultValue(None)]
public enum Orientation
{
None = -1,
North = 0,
East = 1,
South = 2,
West = 3
}
然后在代码中可以使用
public Orientation GetDefaultOrientation()
{
return default(Orientation);
}
enum Orientations
{
None, North, East, South, West
}
private Orientations? _orientation { get; set; }
public Orientations? Orientation
{
get
{
return _orientation ?? Orientations.None;
}
set
{
_orientation = value;
}
}
如果将属性设置为null,则会返回朝向。没有得到。 属性_orientation默认为空。
如果0不是合适的默认值,你可以使用组件模型为枚举定义一个变通方法:
[DefaultValue(None)]
public enum Orientation
{
None = -1,
North = 0,
East = 1,
South = 2,
West = 3
}
public static class Utilities
{
public static TEnum GetDefaultValue<TEnum>() where TEnum : struct
{
Type t = typeof(TEnum);
DefaultValueAttribute[] attributes = (DefaultValueAttribute[])t.GetCustomAttributes(typeof(DefaultValueAttribute), false);
if (attributes != null &&
attributes.Length > 0)
{
return (TEnum)attributes[0].Value;
}
else
{
return default(TEnum);
}
}
}
然后你可以调用:
Orientation o = Utilities.GetDefaultValue<Orientation>();
System.Diagnostics.Debug.Print(o.ToString());
注意:你需要在文件顶部包含以下一行:
using System.ComponentModel;
这不会改变枚举的实际c#语言默认值,但提供了一种方法来指示(并获得)所需的默认值。
enum的默认值是等于0的枚举成员或第一个元素(如果没有指定value)…但我在我的项目中使用枚举遇到了严重的问题,并通过做下面的事情来克服…无论我的需求是如何与阶级水平相关的……
class CDDtype
{
public int Id { get; set; }
public DDType DDType { get; set; }
public CDDtype()
{
DDType = DDType.None;
}
}
[DefaultValue(None)]
public enum DDType
{
None = -1,
ON = 0,
FC = 1,
NC = 2,
CC = 3
}
并得到预期的结果
CDDtype d1= new CDDtype();
CDDtype d2 = new CDDtype { Id = 50 };
Console.Write(d1.DDType);//None
Console.Write(d2.DDType);//None
现在,如果价值来自DB ....在这种情况下……将值传递给下面的函数,它会将值转换为enum…下面的函数处理了各种场景,它是通用的…相信我,这是非常快.....:)
public static T ToEnum<T>(this object value)
{
//Checking value is null or DBNull
if (!value.IsNull())
{
return (T)Enum.Parse(typeof(T), value.ToStringX());
}
//Returanable object
object ValueToReturn = null;
//First checking whether any 'DefaultValueAttribute' is present or not
var DefaultAtt = (from a in typeof(T).CustomAttributes
where a.AttributeType == typeof(DefaultValueAttribute)
select a).FirstOrNull();
//If DefaultAttributeValue is present
if ((!DefaultAtt.IsNull()) && (DefaultAtt.ConstructorArguments.Count == 1))
{
ValueToReturn = DefaultAtt.ConstructorArguments[0].Value;
}
//If still no value found
if (ValueToReturn.IsNull())
{
//Trying to get the very first property of that enum
Array Values = Enum.GetValues(typeof(T));
//getting very first member of this enum
if (Values.Length > 0)
{
ValueToReturn = Values.GetValue(0);
}
}
//If any result found
if (!ValueToReturn.IsNull())
{
return (T)Enum.Parse(typeof(T), ValueToReturn.ToStringX());
}
return default(T);
}
enum(实际上,任何值类型)的默认值是0——即使这不是该enum的有效值。这是无法改变的。