简体   繁体   English

如何使用System.Reflection.Metadata获取属性类型?

[英]How to get type of property using System.Reflection.Metadata?

I'm using System.Reflection.Metadata to enumerate some assemblies and pull out some information. 我正在使用System.Reflection.Metadata来枚举一些程序集并提取一些信息。 For instance, I need to locate a type and then get information about its properties (name and type). 例如,我需要找到一个类型,然后获取有关其属性(名称和类型)的信息。 I can find the type, find the property, but I can't figure out how to determine the type of the property. 我可以找到类型,找到属性,但我无法弄清楚如何确定属性的类型 Here's what I have so far: 这是我到目前为止所拥有的:

using (Stream stream = File.OpenRead(assemblyPath))
using (PEReader reader = new PEReader(stream))
{
    TypeDefinition type = ...;
    var properties = (from handles in type.GetProperties()
                      let prop = metadata.GetPropertyDefinition(handles)
                      let accessors = prop.GetAccessors()
                      let getter = metadata.GetMethodDefinition(accessors.Getter)
                      where getter.Attributes.HasFlag(MethodAttributes.Public)
                      select new
                      {
                          Name = prop.Name,
                          //Type=prop. ??? getter. ??? What to do here?
                      });

    foreach (var prop in properties)
        yield return prop;
}

For now I will simply load up the type using actual reflection in order to get the properties. 现在我将简单地使用实际反射加载类型以获取属性。 But I'd rather use the metadata stuff instead. 但我宁愿使用元数据。

Probably too late for you, but if someone else comes along, here's an answer: 对你来说可能为时已晚,但如果有其他人出现,这是一个答案:

prop.Signature is the type of the property and you can call prop.DecodeSignature(...) with an ISignatureTypeProvider of your choice (you will most likely have to implement one though) prop.Signature是属性的类型,你可以使用你选择的ISignatureTypeProvider调用prop.DecodeSignature(...) (你很可能必须实现一个)

The reason why its so complex is that the property type can refer to generic parameters of the containing type. 它如此复杂的原因是属性类型可以引用包含类型的泛型参数。 The System.Reflection.Metadata library is low level and doesn't come with an integrated type system so the first thing you want to do is decide how to translate types into something you can use, and implement ISignatureTypeProvider accordingly. System.Reflection.Metadata库是低级别的,并没有集成类型系统,因此您要做的第一件事就是决定如何将类型转换为可以使用的类型,并相应地实现ISignatureTypeProvider

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 System.Reflection.Metadata 查找实现特定接口的所有类 - How to find all classes that implement a specific interface using System.Reflection.Metadata 无法从程序集System.Reflection.Metadata加载类型'System.Reflection.Metadata.ISignatureTypeProvider - Could not load type 'System.Reflection.Metadata.ISignatureTypeProvider from assembly System.Reflection.Metadata 从 C# 8 中的 System.Reflection.Metadata 获取接口可为空的上下文元数据 - Getting interface nullable context metadata from System.Reflection.Metadata in C# 8 尝试生成语义模型时无法加载文件或程序集'System.Reflection.Metadata - Could not load file or assembly 'System.Reflection.Metadata while trying to generate Semantic Model 使用反射从元数据类中获取属性属性 - Using reflection to get property attributes from a metadata class 如何使用反射获取属性? - How to get a property using reflection? 如何从 System.Reflection.MethodBase.MetadataToken 获取 System.Reflection.Metadata.MethodDefintion? - How to get a System.Reflection.Metadata.MethodDefintion from System.Reflection.MethodBase.MetadataToken? 如何使用反射获取继承属性的值? - How to get value of inherited property using reflection? 如何使用反射获取属性值 - How to get a property value using reflection 使用反射获取类型 - Get type using reflection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM