C# 将属性属性值的TypeOf标识为现有类名

C# 将属性属性值的TypeOf标识为现有类名,c#,reflection,C#,Reflection,我试图遍历那些属性没有链接到其他类的类(例如。 公共SomeOtherClass代码{get;set;} 在我的尝试中,并感谢其他人的建议,我提出了以下内容,它遍历特定命名空间的类(即Library=“ConsoleApp1.Library”等),如下所示: 并将属性作为 var properties = classes.SelectMany (x => x.GetProperties (BindingFlags.Public | BindingFlags.Instance

我试图遍历那些属性没有链接到其他类的类(例如。 公共SomeOtherClass代码{get;set;}

在我的尝试中,并感谢其他人的建议,我提出了以下内容,它遍历特定命名空间的类(即Library=“ConsoleApp1.Library”等),如下所示:

并将属性作为

        var properties = classes.SelectMany (x => x.GetProperties (BindingFlags.Public | BindingFlags.Instance)
                                .Select (y => y.PropertyType));
当我从foreach循环中获得所需的信息时:

        foreach ( var method in classes.Where 
                (x => !properties.Contains(x) && !properties.Contains (x))
                .Select (x => x.Name) )
        {
        // some work here
        }
但是,有些情况下,我的选择忽略了;有些情况下,该类具有以下属性,如Array、ICollection、List和Dictionary:

public class TopObject
{
    [JsonProperty("ex")]
    public OtherResults ex { get; set; }

    [JsonProperty ("Results")]
    public OtherResults Results { get; set; }

    private readonly OtherResults[,] Codes1 = new OtherResults[9, 9];
    public ICollection<OtherResults> Codes2 { get; set; }
    public List<OtherResults> Codes3 { get; set; }
    public Dictionary<int, OtherResults> Map { get { return _map; } }

    public TopObject()
    { Results = new OtherResults (); }
}

public class OtherResults
{
    [JsonProperty ("Jcodes")]
    public string Jcodes { get; set; }

    public OtherResults()
    { }
}
公共类拓扑对象
{
[JsonProperty(“ex”)]
public OtherResults ex{get;set;}
[JsonProperty(“结果”)]
公共OtherResults{get;set;}
private readonly OtherResults[,]Codes1=新的OtherResults[9,9];
公共ICollection代码2{get;set;}
公共列表代码3{get;set;}
公共字典映射{get{return\u Map;}}
公共拓扑对象()
{Results=new OtherResults();}
}
公开课成绩
{
[JsonProperty(“Jcodes”)]
公共字符串Jcodes{get;set;}
公共成果
{ }
}
我需要帮助编辑
var方法
,以包括(另外)属性具有
字典
值类型的任何类,或者是
数组
类型的任何类,或者是接受任何类的
ICollection
列表
的情况


有人能帮我做这件事吗?非常感谢你,所以我终于有时间玩一下了。如果我理解正确,你会喜欢这样的东西:

var currentAssembly = typeof(Program).Assembly;

var currentAssemblyName = typeof(Program).Assembly.GetName().Name;

var types = currentAssembly.GetTypes();

var classes = types.Where(type => type.IsClass && !type.IsNested).ToList();

var referencedTypes = classes.SelectMany(c => c.GetProperties().SelectMany(p => GetReferencedTypes(p.PropertyType))).Where(type => type.Assembly.GetName().Name == currentAssemblyName).Select(type => type.Name)
    .Distinct().ToList();
首先,我们创建一个helper函数,用于提取所有引用的类型:

public static IEnumerable<Type> GetReferencedTypes(Type type)
{
    if (type.IsArray)
        return new List<Type> { type, type.GetElementType() };
    if (type.IsGenericType)
        return type.GetGenericArguments().SelectMany(GetReferencedTypes)
            .Union(new List<Type>{type});
    return new List<Type>{ type };
}
我们获取当前程序集。获取所有类型,然后通过helper函数放置这些类型的所有属性,选择它们的名称,删除重复项并创建一个列表。这将导致:

["Method100Response201_01", "Method600Response100", "Method700Response100", "Method100Response404_01"]
请记住,在您的小提琴更改中,
public Method500Response100[,]Codes1=newmethod500response100[9,9];
不是由
GetProperties()
调用返回的(它没有
{get;set;}


希望这对任何类型的对象集合或泛型中的任何引用都有帮助。
列表如何?
?我假设这也算作对
其他结果的引用。
?这说明了如何获取泛型类型参数,对要支持的特定泛型进行筛选或检查l:@Knoop非常感谢您查找此内容;您完全正确,是的算作参考,我想将其从我的结果中排除。@Knoop:谢谢您的链接,非常有趣;我只是不知道如何从
type.IsGenericType&&type.GetGenericTypeDefinition()==typeof(列表)开始
实际检查所有这些类型(数组、字典等)关于我的类的类型。但我明白你的意思。我感觉你不关心它是什么类型的泛型,你只想深入研究泛型类型参数。如果一个类有一个泛型属性,但与列表无关,我假设你仍然想排除该泛型定义中引用的任何类。所以我将跳过整个类型。GetGenericTypeDefinition()==typeof(列表),只提取
GetGenericArguments
返回的类型。请记住,这些类型本身也可以是泛型,因此您必须递归地执行此操作。为延迟响应道歉,非常感谢您查看如此复杂的问题,我正在尽快测试。我用建议的解决方案更新了fiddle(好眼力!!谢谢你发现丢失的getsetter!)然后你可以看一下。所以,问题是,我得到了一个带有数组符号的类名,比如,
Method500Response100[]
然后又是
Method500Response100
Method500Response100
不过是正确的。但不幸的是,抛出的两个类也被引用,在小提琴中更为明显(我没有更改模型,与示例相同,可能名称有所不同:集合仍然包括IList、Dict、ICollection和array等被引用的类。仅此而已)。我刚刚做了一个foreach循环,如果还有什么我应该做的,请纠正我。我认为您非常接近一个确定的解决方案,并且比我的解决方案要好,因此,再次感谢您查找该解决方案。这不是一个bug。
public Method500Response100[]Codes1{get;set;}
此属性属于
Method500Response100[]
并且根据您的规范,它引用了
Method500Response100
类型,这就是为什么两者都被添加的原因。这是通过这行
返回新列表{type,type.GetElementType()};
如果出于某种原因您不想要数组类型,并且只有它们引用的类型才将其更改为
返回新列表{type.GetElementType()};
。我的回复中忘了提到你@Nick
["Method100Response201_01", "Method600Response100", "Method700Response100", "Method100Response404_01"]