C# Assembly Reflection

The System.Reflection namespace is tool that allows you to reflect the properties of an assembly without importing the assembly to your project references. A good example is the RESTful application I host on GitHub.

Reflected Assembly

This code (below) will load an assembly from the specified file path in a WPF application.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Reflection;

namespace RESTful
{
    class ReflectedAssembly
    {
        private static Assembly _ReflectedAssembly = null;
        public static Assembly Assembly
        {
            get
            {
                // Get the path from x:Name="AssemblyPath"
                string assemblyPath = ((MainWindow)System.Windows.Application.Current.MainWindow).AssemblyPath.Text;

                if ((_ReflectedAssembly == null) && (assemblyPath != null) && (assemblyPath != ""))
                {
                    // Load the imported assembly
                    _ReflectedAssembly = Assembly.LoadFrom(assemblyPath);
                }
                return _ReflectedAssembly;
            }
        }
    }

}

07072014_01

Reflected Assembly Properties

Now that the assembly is loaded, you can see it’s properties.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Reflection;

namespace RESTful
{
    class ReflectedProperties
    {
        public static PropertyInfo[] _Properties = null;
        public static PropertyInfo[] Properties
        {
            get
            {
                if (ReflectedType.Type != null)
                {
                    // Get the properties for the selected type
                    _Properties = ReflectedType.Type.GetProperties();
                }

                return _Properties;
            }
        }
    }
}

07072014_02

Reflected Assembly Property Type

Now that there is a list of Properties, we can select one and view its data type.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Reflection;

namespace RESTful
{
    class ReflectedType
    {
        public static Type _ReflectedType = null;
        public static Type Type
        {
            get
            {
                if (ReflectedAssembly.Assembly != null)
                {
                    // Get the selected value from x:Name="Types"
                    string selectedType = ((MainWindow)System.Windows.Application.Current.MainWindow).Types.SelectedValue.ToString();

                    // Get the type
                    _ReflectedType = ReflectedAssembly.Assembly.GetType(selectedType);
                }

                return _ReflectedType;
            }
        }

    }
}

07072014_03

Further Reading

MSDN

  • Microsoft’s documentation on the System.Reflection namespace

GitHub

  • My project (RESTful) that is used as examples in this blog post

.Net Reflector

  • An application that uses reflection (not open source) to give you some ideas