关于C#反射 你需要知道的

2020-06-09 19:00:23王冬梅

反射获取自定义特性

以下是四个常见的场景示例。

示例一,找出一个类中标注了某个自定义特性(比如 MyAtrribute)的属性。

var props = type
  .GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance)
  .Where(prop =>Attribute.IsDefined(prop, typeof(MyAttribute)));

示例二,找出某个属性的所有自定义特性。

var attributes = typeof(t).GetProperty("Name").GetCustomAttributes(false);

示例三,找出程序集所有标注了某个自定义特性的类。

static IEnumerable<Type> GetTypesWithAttribute(Assembly assembly)
{
  foreach(Type type inassembly.GetTypes())
  {
    if (type.GetCustomAttributes(typeof(MyAttribute), true).Length > 0)
    {
      yield return type;
    }
  }
}

示例四,在运行时读取自定义特性的值

public static class AttributeExtensions
{
  public static TValue GetAttribute<TAttribute, TValue>(
    this Type type,
    string MemberName,
    Func<TAttribute, TValue> valueSelector,
    bool inherit = false)
    where TAttribute : Attribute
  {
    var att = type.GetMember(MemberName).FirstOrDefault()
      .GetCustomAttributes(typeof(TAttribute), inherit)
      .FirstOrDefault() as TAttribute;
    if (att != null)
    {
      return valueSelector(att);
    }
    return default;
  }
}

// 使用:

class Program
{
  static void Main()
  {
    // 读取 MyClass 类的 MyMethod 方法的 Description 特性的值
    var description = typeof(MyClass)
      .GetAttribute("MyMethod", (DescriptionAttribute d) => d.Description);
    Console.WriteLine(description); // 输出:Hello
  }
}
public class MyClass
{
  [Description("Hello")]
  public void MyMethod() { }
}

动态实例化接口的所有实现类(插件激活)

通过反射来动态实例化某个接口的所有实现类,常用于实现系统的插件式开发。比如在程序启动的时候去读取指定文件夹(如 Plugins)中的 dll 文件,通过反射获取 dll 中所有实现了某个接口的类,并在适当的时候将其实例化。大致实现如下:

interface IPlugin
{
  string Description { get; }
  void DoWork();
}

某个在独立 dll 中的类:

class HelloPlugin : IPlugin
{
  public string Description => "A plugin that says Hello";
  public void DoWork()
  {
    Console.WriteLine("Hello");
  }
}

在你的系统启动的时候动态加载该 dll,读取实现了 IPlugin 接口的所有类的信息,并将其实例化。

public IEnumerable<IPlugin> InstantiatePlugins(string directory)
{
  var assemblyNames = Directory.GetFiles(directory, "*.addin.dll")
    .Select(name => new FileInfo(name).FullName).ToArray();

  foreach (var fileName assemblyNames)
    AppDomain.CurrentDomain.Load(File.ReadAllBytes(fileName));

  var assemblies = assemblyNames.Select(System.Reflection.Assembly.LoadFile);
  var typesInAssembly = assemblies.SelectMany(asm =>asm.GetTypes());
  var pluginTypes = typesInAssembly.Where(type => typeof (IPlugin).IsAssignableFrom(type));

  return pluginTypes.Select(Activator.CreateInstance).Cast<IPlugin>();
}