मैं हाल ही में यह रिवाज मेरी enum में खेतों विशेषताएं जोड़ने के लिए इस्तेमाल किया:
public enum ShapeName
{
// Lines
[ShapeDescription(ShapeType.Line, "Horizontal Scroll Distance", "The horizontal distance to scroll the browser in order to center the game.")]
HorizontalScrollBar,
[ShapeDescription(ShapeType.Line, "Vertical Scroll Distance", "The vertical distance to scroll the browser in order to center the game.")]
VerticalScrollBar,
}
प्रतिबिंब का उपयोग क्षेत्र पाने के लिए:
public static ShapeDescriptionAttribute GetShapeDescription(this ShapeName shapeName)
{
Type type = shapeName.GetType();
FieldInfo fieldInfo = type.GetField(shapeName.ToString());
ShapeDescriptionAttribute[] attribs = fieldInfo.GetCustomAttributes(typeof(ShapeDescriptionAttribute), false) as ShapeDescriptionAttribute[];
return (attribs != null && attribs.Length > 0) ? attribs[0] : new ShapeDescriptionAttribute(ShapeType.NotSet, shapeName.ToString());
}
विशेषता वर्ग:
[AttributeUsage(AttributeTargets.Field)]
public class ShapeDescriptionAttribute: Attribute
{
#region Constructor
public ShapeDescriptionAttribute(ShapeType shapeType, string name) : this(shapeType, name, name) { }
public ShapeDescriptionAttribute(ShapeType shapeType, string name, string description)
{
Description = description;
Name = name;
Type = shapeType;
}
#endregion
#region Public Properties
public string Description { get; protected set; }
public string Name { get; protected set; }
public ShapeType Type { get; protected set; }
#endregion
}
स्रोत
2009-12-13 21:06:34
संबंधित प्रश्न: http://stackoverflow.com/questions/1859902/in-3-minutes-what-is-reflection –
संबंधित हां, लेकिन मुझे व्यक्तिगत रूप से लगता है कि प्रश्न बल्कि अनुपयोगी है। एकीकरण रिवर्स व्युत्पन्न कैलकुस है, लेकिन मैं आपको बता रहा हूं कि आपको कुछ भी नहीं सिखाया गया है, भले ही आपको पता चले कि व्युत्पन्न क्या है। : डी –