Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reflection. What can we achieve using it?

I'm reading and learning about reflection in C#. It would be fine to know how can it help me in my daily work, so I want people with more experience than me tell me samples or ideas about what kinds of things can we achieve using it, or how can we reduce de amount of code that we write.

Thanks.

like image 390
Jonathan Avatar asked Dec 13 '09 20:12

Jonathan


3 Answers

One use among many: you can make a plug-in architecture where you specify the name of the class-to-use in a configuration file. Using reflection you can take this string and create an instance of the object requested. If that object implements a known interface, then you can use it through ordinary (non reflection) code.

like image 192
Hans Kesting Avatar answered Sep 23 '22 04:09

Hans Kesting


I recently used it to add custom attributes to fields in my 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,
}

Using reflection to get the field:

    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());
    }

The attribute class:

[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
}
like image 11
Cory Charlton Avatar answered Nov 15 '22 12:11

Cory Charlton


Generally speaking Reflection allows you access to metadata about objects. Combining Reflection with other techniques allows you to make your program more dynamic. For instance you can load a DLL and determine if it contains an implementation of an interface. You could use this to discover dll's that support functionality at runtime. Use could use this to extend an application without a recompilation and without having to restart it.

Intellisense in Visual Studio uses reflection to give you information about the objects you are using.

Note that using Reflection comes at a cost. Reflecting an object can be slow. But if you need it Reflection is a very usefull tool.

like image 10
Jeffrey Hines Avatar answered Nov 15 '22 12:11

Jeffrey Hines