使用枚举 Class
Using Enumeration Class
我是 C# 的新手,对抽象 classes 和继承也比较陌生,我在理解如何使用它们时遇到了麻烦。我有这个抽象枚举 class:
public abstract class Enumeration : IComparable
{
public uint Id { get; private set; }
public string Name { get; private set; }
public uint MaxTemperature { get; private set; }
public double Density { get; private set; }
protected Enumeration()
{
}
protected Enumeration(uint id, string name, uint maxTemprature, double density)
{
Id = id;
Name = name;
MaxTemperature = maxTemprature;
Density = density;
}
public static IEnumerable<T> GetAll<T>() where T : Enumeration,
new()
{
var type = typeof(T);
var fields = type.GetTypeInfo().GetFields(BindingFlags.Public
| BindingFlags.Static | BindingFlags.DeclaredOnly);
foreach (var info in fields)
{
var instance = new T();
var locatedValue = info.GetValue(instance) as T;
if (locatedValue != null)
{
yield return locatedValue;
}
}
}
public override bool Equals(object obj)
{
var otherValue = obj as Enumeration;
if (otherValue == null)
{
return false;
}
var typeMatches = GetType().Equals(obj.GetType());
var valueMatches = Id.Equals(otherValue.Id);
return typeMatches && valueMatches;
}
public int CompareTo(object other)
{
return Id.CompareTo(((Enumeration)other).Id);
}
public override int GetHashCode()
{
return Id.GetHashCode();
}
}
这个class是我的material继承的 class:
class Material : Enumeration
{
public static readonly Material FreeSpace =
new Material(0, "Free Space", 0, 0);
public static readonly Material CarbonSteel =
new Material(1, "Carbon Steel", 2500, 0.284);
private Material()
{
}
private Material(uint id, string name, uint maxTemperature,
double density) : base(id, name, maxTemperature, density)
{
}
private static IEnumerable<Material> List()
{
return new[] { FreeSpace, CarbonSteel };
}
}
现在我想在我的部分使用这些materialclass:
class Part
{
private Material partMaterial;
public Part() { }
public Material PartMaterial
{
set
{
partMaterial = value;
}
}
}
这就是我卡住的地方,如何将变量设置为枚举的静态对象之一,以便从中获取属性?
您可以使用 SelectedItem 而不是 SelectedIndex
part.PartMaterial = (Material) MaterialCombo.SelectedItem;
所以,我希望我能把问题保持原样,因为最终这是提出问题的正确方式。但是在冷嘲热讽的评论和降级之后,我把它改成了我认为更好的。原问题的回答方式应该是:
由于您正在枚举 materials class,您需要一种方法来公开对象的枚举值。这
IEnumerable<Material> List()
方法应该 public 来完成这个。
然后您可以使用 MaterialCombo.DataSource = Material.List()
使用 material 对象填充组合框,并使用 MaterialCombo.DisplayMember = "Name";
在组合框中显示这些对象的名称。
最后,使用@Oxald 的回答将 material 传递给您的部分 class。
感谢@Mark Benningfield 为我指明了搜索 "Using an enum to populate a combobox" 的方向,这对我很有帮助。
Oxald 建议使用 .SelectedItem 而不是 .SelectedIndex。
我是 C# 的新手,对抽象 classes 和继承也比较陌生,我在理解如何使用它们时遇到了麻烦。我有这个抽象枚举 class:
public abstract class Enumeration : IComparable
{
public uint Id { get; private set; }
public string Name { get; private set; }
public uint MaxTemperature { get; private set; }
public double Density { get; private set; }
protected Enumeration()
{
}
protected Enumeration(uint id, string name, uint maxTemprature, double density)
{
Id = id;
Name = name;
MaxTemperature = maxTemprature;
Density = density;
}
public static IEnumerable<T> GetAll<T>() where T : Enumeration,
new()
{
var type = typeof(T);
var fields = type.GetTypeInfo().GetFields(BindingFlags.Public
| BindingFlags.Static | BindingFlags.DeclaredOnly);
foreach (var info in fields)
{
var instance = new T();
var locatedValue = info.GetValue(instance) as T;
if (locatedValue != null)
{
yield return locatedValue;
}
}
}
public override bool Equals(object obj)
{
var otherValue = obj as Enumeration;
if (otherValue == null)
{
return false;
}
var typeMatches = GetType().Equals(obj.GetType());
var valueMatches = Id.Equals(otherValue.Id);
return typeMatches && valueMatches;
}
public int CompareTo(object other)
{
return Id.CompareTo(((Enumeration)other).Id);
}
public override int GetHashCode()
{
return Id.GetHashCode();
}
}
这个class是我的material继承的 class:
class Material : Enumeration
{
public static readonly Material FreeSpace =
new Material(0, "Free Space", 0, 0);
public static readonly Material CarbonSteel =
new Material(1, "Carbon Steel", 2500, 0.284);
private Material()
{
}
private Material(uint id, string name, uint maxTemperature,
double density) : base(id, name, maxTemperature, density)
{
}
private static IEnumerable<Material> List()
{
return new[] { FreeSpace, CarbonSteel };
}
}
现在我想在我的部分使用这些materialclass:
class Part
{
private Material partMaterial;
public Part() { }
public Material PartMaterial
{
set
{
partMaterial = value;
}
}
}
这就是我卡住的地方,如何将变量设置为枚举的静态对象之一,以便从中获取属性?
您可以使用 SelectedItem 而不是 SelectedIndex
part.PartMaterial = (Material) MaterialCombo.SelectedItem;
所以,我希望我能把问题保持原样,因为最终这是提出问题的正确方式。但是在冷嘲热讽的评论和降级之后,我把它改成了我认为更好的。原问题的回答方式应该是:
由于您正在枚举 materials class,您需要一种方法来公开对象的枚举值。这
IEnumerable<Material> List()
方法应该 public 来完成这个。
然后您可以使用 MaterialCombo.DataSource = Material.List()
使用 material 对象填充组合框,并使用 MaterialCombo.DisplayMember = "Name";
在组合框中显示这些对象的名称。
最后,使用@Oxald 的回答将 material 传递给您的部分 class。
感谢@Mark Benningfield 为我指明了搜索 "Using an enum to populate a combobox" 的方向,这对我很有帮助。
Oxald 建议使用 .SelectedItem 而不是 .SelectedIndex。