gpt4 book ai didi

c# - 为类型创建指令

转载 作者:行者123 更新时间:2023-11-30 16:04:18 25 4
gpt4 key购买 nike

使用 Mono.Cecil,给出这个方法

private Instruction LoadOnStack(MetadataType type, object value)
{
switch (type)
{
case MetadataType.String:
return _processor.Create(OpCodes.Ldstr, (string) value);
case MetadataType.Int32:
return _processor.Create(OpCodes.Ldc_I4, (Int32) value);
case MetadataType.Int64:
return _processor.Create(OpCodes.Ldc_I8, (Int64) value);
case MetadataType.Boolean:
return _processor.Create(OpCodes.Ldc_I4, (bool) value ? 1 : 0);
}

throw new NotSupportedException("Not a supported primitve parameter type: " + type);
}

value 的类型为 Type 时,如何创建可以加载 valueInstruction

我注意到当 valueType 类型时,我可以像这样测试它:

if (value is TypeReference)
return _processor.Create(???, ???);

但我不知道我需要将什么传递给 Create 以获取正确加载的值。

编辑:

使用这个:

if (value is TypeReference)
return _processor.Create(OpCodes.Ldobj, type.Resolve());

让我更近一步。它似乎接受类型。但是当我尝试编写程序集时,它会出错:

System.ArgumentException : Member 'System.Type' is declared in another module and needs to be imported

最佳答案

正如@cubrr 已经指出的那样:

我们将此代码用于 MethodBoundaryAspect.Fody

private IList<Instruction> LoadValueOnStack(TypeReference parameterType, object value, ModuleDefinition module)
{
if (parameterType.IsPrimitive || (parameterType.FullName == "System.String"))
return new List<Instruction> {LoadPrimitiveConstOnStack(parameterType.MetadataType, value)};

if (parameterType.IsValueType) // enum
{
var enumUnderlyingType = GetEnumUnderlyingType(parameterType.Resolve());
return new List<Instruction> {LoadPrimitiveConstOnStack(enumUnderlyingType.MetadataType, value)};
}

if (parameterType.FullName == "System.Type")
{
var typeName = value.ToString();
var typeReference = module.GetType(typeName, true);

var typeTypeRef = _referenceFinder.GetTypeReference(typeof (Type));
var methodReference = _referenceFinder.GetMethodReference(typeTypeRef, md => md.Name == "GetTypeFromHandle");

var instructions = new List<Instruction>
{
_processor.Create(OpCodes.Ldtoken, typeReference),
_processor.Create(OpCodes.Call, methodReference)
};

return instructions;
}

throw new NotSupportedException("Parametertype: " + parameterType);
}

private Instruction LoadPrimitiveConstOnStack(MetadataType type, object value)
{
switch (type)
{
case MetadataType.String:
return _processor.Create(OpCodes.Ldstr, (string) value);
case MetadataType.Int32:
return _processor.Create(OpCodes.Ldc_I4, (int) value);
case MetadataType.Int64:
return _processor.Create(OpCodes.Ldc_I8, (long) value);
case MetadataType.Boolean:
return _processor.Create(OpCodes.Ldc_I4, (bool) value ? 1 : 0);
}

throw new NotSupportedException("Not a supported primitive parameter type: " + type);
}

private static TypeReference GetEnumUnderlyingType(TypeDefinition self)
{
foreach (var field in self.Fields)
{
if (field.Name == "value__")
return field.FieldType;
}

throw new ArgumentException();
}

ReferenceFinder 类在哪里:

private readonly ModuleDefinition _moduleDefinition;

public ReferenceFinder(ModuleDefinition moduleDefinition)
{
_moduleDefinition = moduleDefinition;
}

public MethodReference GetMethodReference(Type declaringType, Func<MethodDefinition, bool> predicate)
{
return GetMethodReference(GetTypeReference(declaringType), predicate);
}

public MethodReference GetMethodReference(TypeReference typeReference, Func<MethodDefinition, bool> predicate)
{
var typeDefinition = typeReference.Resolve();

MethodDefinition methodDefinition;
do
{
methodDefinition = typeDefinition.Methods.FirstOrDefault(predicate);
typeDefinition = typeDefinition.BaseType == null
? null
: typeDefinition.BaseType.Resolve();
} while (methodDefinition == null && typeDefinition != null);

return _moduleDefinition.Import(methodDefinition);
}

public TypeReference GetTypeReference(Type type)
{
return _moduleDefinition.Import(type);
}

关于c# - 为类型创建指令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35215430/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com