"unmanaged" IL 中的通用参数约束
"unmanaged" generic parameter constraint in IL
当我编译以下代码时:
type Class1<'T when 'T : unmanaged> =
class end
type Class2<'T> =
class end
在 IL 中它看起来像这样:
.class auto ansi serializable nested public Class1`1<T> extends [mscorlib]System.Object
.class auto ansi serializable nested public Class2`1<T> extends [mscorlib]System.Object
unmanaged
约束是否保存在某处?如果是这样,我在哪里以及如何以编程方式获取它?
在this question,我读到它是在"emdbedded signature file"的某个地方。那是什么?
这些约束位于存储在 FSharpSignatureData 托管资源中的实体数据中。可以使用来自 F# PowerPack 的 F# 元数据 Reader 浏览它们:
public static IList<FSharpGenericParameterConstraint> GetFSharpGenericParameterConstraints(Type genericType, int position)
{
return GetFSharpGenericParameterConstraints(genericType.GetGenericArguments()[position]);
}
public static IList<FSharpGenericParameterConstraint> GetFSharpGenericParameterConstraints(Type genericParameter)
{
var declMethod = genericParameter.DeclaringMethod;
var declType = genericParameter.DeclaringType;
var fsAsm = FSharpAssembly.FromAssembly(genericParameter.Assembly);
int pos = genericParameter.GenericParameterPosition;
var entities = AllEntities(fsAsm.Entities);
if(declMethod != null)
{
var member = entities.SelectMany(e => e.MembersOrValues).First(m => m.ReflectionMemberInfo == declMethod);
return member.GenericParameters[pos].Constraints;
}else if(declType != null)
{
var entity = entities.First(e => e.ReflectionType == declType);
return entity.GenericParameters[pos].Constraints;
}
return null;
}
private static IEnumerable<FSharpEntity> AllEntities(IEnumerable<FSharpEntity> entities)
{
return entities.Concat(entities.SelectMany(e => AllEntities(e.NestedEntities)));
}
bool unmanaged = GetFSharpGenericParameterConstraints(typeof(MyModule.Class1<>), 0).Any(c => c.IsUnmanagedConstraint);
当我编译以下代码时:
type Class1<'T when 'T : unmanaged> =
class end
type Class2<'T> =
class end
在 IL 中它看起来像这样:
.class auto ansi serializable nested public Class1`1<T> extends [mscorlib]System.Object
.class auto ansi serializable nested public Class2`1<T> extends [mscorlib]System.Object
unmanaged
约束是否保存在某处?如果是这样,我在哪里以及如何以编程方式获取它?
在this question,我读到它是在"emdbedded signature file"的某个地方。那是什么?
这些约束位于存储在 FSharpSignatureData 托管资源中的实体数据中。可以使用来自 F# PowerPack 的 F# 元数据 Reader 浏览它们:
public static IList<FSharpGenericParameterConstraint> GetFSharpGenericParameterConstraints(Type genericType, int position)
{
return GetFSharpGenericParameterConstraints(genericType.GetGenericArguments()[position]);
}
public static IList<FSharpGenericParameterConstraint> GetFSharpGenericParameterConstraints(Type genericParameter)
{
var declMethod = genericParameter.DeclaringMethod;
var declType = genericParameter.DeclaringType;
var fsAsm = FSharpAssembly.FromAssembly(genericParameter.Assembly);
int pos = genericParameter.GenericParameterPosition;
var entities = AllEntities(fsAsm.Entities);
if(declMethod != null)
{
var member = entities.SelectMany(e => e.MembersOrValues).First(m => m.ReflectionMemberInfo == declMethod);
return member.GenericParameters[pos].Constraints;
}else if(declType != null)
{
var entity = entities.First(e => e.ReflectionType == declType);
return entity.GenericParameters[pos].Constraints;
}
return null;
}
private static IEnumerable<FSharpEntity> AllEntities(IEnumerable<FSharpEntity> entities)
{
return entities.Concat(entities.SelectMany(e => AllEntities(e.NestedEntities)));
}
bool unmanaged = GetFSharpGenericParameterConstraints(typeof(MyModule.Class1<>), 0).Any(c => c.IsUnmanagedConstraint);