使用反射的 C# 类型比较

标签 c# reflection comparison

我想检查一个属性是否属于 DbSet<T> 类型使用反射。

public class Foo
{
    public DbSet<Bar> Bars { get; set; }
}

通过使用反射:

var types = Assembly.GetExecutingAssembly().GetTypes();
foreach (var type in types)
{
    if (type.IsSubclassOf(typeof (Foo)) || type.FullName == typeof (Foo).FullName)
    {
        foreach (
            var prop in Type.GetType(type.FullName).
                GetProperties(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance))
        {
            var propType = prop.PropertyType;
            bool a = propType.IsAssignableFrom(typeof (DbSet<>));
            bool b = typeof (DbSet<>).IsAssignableFrom(propType);

            bool c = propType.BaseType.IsAssignableFrom(typeof (DbSet<>));
            bool d = typeof (DbSet<>).IsAssignableFrom(propType.BaseType);


            bool e = typeof (DbSet<>).IsSubclassOf(propType);
            bool f = typeof (DbSet<>).IsSubclassOf(propType.BaseType);
            bool g = propType.IsSubclassOf(typeof (DbSet<>));
            bool h = propType.BaseType.IsSubclassOf(typeof (DbSet<>));

            bool i = propType.BaseType.Equals(typeof (DbSet<>));
            bool j = typeof (DbSet<>).Equals(propType.BaseType);

            bool k = propType.Name == typeof (DbSet<>).Name;
        }
    }
}
  • 是否有检查类型的合并解决方案?如您所见,我正在使用 IsSubClassOf + FullName获取类型为 Foo 的类以及派生自 Foo 的任何其他类.

  • 除了 c,f,k 之外的所有检查(a 到 j)都返回 false。 c,f 将 System.Object 作为 BaseType 返回,这对我没有用。 k,我认为不安全检查。但如果没有找到其他解决方法,我将使用我的方法。在 Debug模式下,propTypeFullName是:

    System.Data.Entity.DbSet`1[[Console1.Bar, ConsoleApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]

    有没有其他方法可以检查propType类型为 DbSet<> ?
    谢谢。

最佳答案

你需要它来处理DbSet<>的子类吗?还有吗?如果没有,您可以使用:

if (propType.IsGenericType &&
    propType.GetGenericTypeDefinition() == typeof(DbSet<>))

完整示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

class Test<T>
{
    public List<int> ListInt32 { get; set; }
    public List<T> ListT { get; set; }
    public string Other { get; set; }
    public Action<string> OtherGeneric { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        var query = from prop in typeof(Test<string>).GetProperties()
                    let propType = prop.PropertyType
                    where propType.IsGenericType &&
                          propType.GetGenericTypeDefinition() == typeof(List<>)
                    select prop.Name;

        foreach (string name in query)
        {
            Console.WriteLine(name);
        }
    }
}

对于子类,您只需要在继承层次结构中向上递归地应用相同的测试。如果您需要测试接口(interface),这将变得更加痛苦。

关于使用反射的 C# 类型比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8452327/

相关文章:

java - 如何在 java 中使用反射将字段转换到特定类?

java - 小程序中 Class.forName( "Prohibited package name: java.util") 上的 "java.util..."

java - 如何比较两个 list<t> 对象?

java - 字符串比较 : individual comparison Vs appended string comparison

c# - PromptDialog Choice with List 对象 Bot Framework

c# - 如何为 WCF 配置 log4net

c# - WCF 自定义绑定(bind)或 basichttpbinding

c# - 重构 Java 周期性任务

java - .class == getClass() 测试失败

javascript - if(x>y) { x=y} 在 javaScript 中的简写