本文整理汇总了C#中Mono.Cecil.FieldDefinition.FixFieldReferenceIfDeclaringTypeIsGeneric方法的典型用法代码示例。如果您正苦于以下问题:C# FieldDefinition.FixFieldReferenceIfDeclaringTypeIsGeneric方法的具体用法?C# FieldDefinition.FixFieldReferenceIfDeclaringTypeIsGeneric怎么用?C# FieldDefinition.FixFieldReferenceIfDeclaringTypeIsGeneric使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Mono.Cecil.FieldDefinition
的用法示例。
在下文中一共展示了FieldDefinition.FixFieldReferenceIfDeclaringTypeIsGeneric方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateLoggerStaticField
private FieldReference CreateLoggerStaticField()
{
var logTypeRef = _typeReferenceProvider.LogAdapterReference;
var logManagerTypeRef = _typeReferenceProvider.LogManagerReference;
//look for existing one
var loggerField = _typeDefinition.Fields.FirstOrDefault(fld => fld.IsStatic && fld.FieldType == logTypeRef);
if (loggerField != null) return loggerField.FixFieldReferenceIfDeclaringTypeIsGeneric();
//$log should be unique
loggerField = new FieldDefinition("$log", FieldAttributes.Private | FieldAttributes.Static, logTypeRef);
_typeDefinition.Fields.Add(loggerField);
//create field init
var staticConstructor = _typeDefinition.GetStaticConstructor();
if (staticConstructor == null)
{
const MethodAttributes methodAttributes = MethodAttributes.Private | MethodAttributes.Static | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName;
staticConstructor = new MethodDefinition(".cctor", methodAttributes, _typeReferenceProvider.Void);
_typeDefinition.Methods.Add(staticConstructor);
staticConstructor.Body.Instructions.Add(Instruction.Create(OpCodes.Ret));
}
var getLoggerMethod = new MethodReference("GetLogger", logTypeRef, logManagerTypeRef);
getLoggerMethod.Parameters.Add(new ParameterDefinition(_typeReferenceProvider.Type));
//build up typeInfo
var getTypeFromHandleMethod = _methodReferenceProvider.GetGetTypeFromHandleReference();
//spec treatment for generic types
var loggerFieldRef = loggerField.FixFieldReferenceIfDeclaringTypeIsGeneric();
//if generated nested type use the declaring type as logger type as it is more natural from
//end users perspective
var loggerTypeDefinition = HasCompilerGeneratedAttribute && _typeDefinition.IsNested
? _typeDefinition.DeclaringType : _typeDefinition;
staticConstructor.Body.InsertAtTheBeginning(new[]
{
Instruction.Create(OpCodes.Ldtoken, loggerTypeDefinition.GetGenericInstantiationIfGeneric()),
Instruction.Create(OpCodes.Call, getTypeFromHandleMethod),
Instruction.Create(OpCodes.Call, getLoggerMethod),
Instruction.Create(OpCodes.Stsfld, loggerFieldRef)
});
return loggerFieldRef;
}