C#: Even more reflection for retrieving meta data handle

This commit is contained in:
Tom Hvitved
2020-09-11 11:40:53 +02:00
parent 2cc635f7e0
commit f225a17639

View File

@@ -136,16 +136,32 @@ namespace Semmle.Extraction.CSharp.Entities
trapFile.metadata_handle(this, Location, MetadataTokens.GetToken(handle.Value));
}
static System.Reflection.PropertyInfo GetPropertyInfo(object o, string name) =>
o.GetType().GetProperty(name, System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetProperty);
public Handle? MetadataHandle
{
get
{
var propertyInfo = symbol.GetType().GetProperty("Handle",
System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetProperty);
var handleProp = GetPropertyInfo(symbol, "Handle");
object handleObj = symbol;
if (propertyInfo != null)
if (handleProp is null)
{
switch (propertyInfo.GetValue(symbol))
var underlyingSymbolProp = GetPropertyInfo(symbol, "UnderlyingSymbol");
if (underlyingSymbolProp is object)
{
if (underlyingSymbolProp.GetValue(symbol) is object underlying)
{
handleProp = GetPropertyInfo(underlying, "Handle");
handleObj = underlying;
}
}
}
if (handleProp is object)
{
switch (handleProp.GetValue(handleObj))
{
case MethodDefinitionHandle md: return md;
case TypeDefinitionHandle td: return td;