当前位置: 首页>>代码示例>>C#>>正文


C# FieldDefinition.FixFieldReferenceIfDeclaringTypeIsGeneric方法代码示例

本文整理汇总了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;
        }
开发者ID:ravibpathuri,项目名称:tracer,代码行数:48,代码来源:TypeWeaver.cs


注:本文中的Mono.Cecil.FieldDefinition.FixFieldReferenceIfDeclaringTypeIsGeneric方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。