• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C# TypeDefinition类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了C#中TypeDefinition的典型用法代码示例。如果您正苦于以下问题:C# TypeDefinition类的具体用法?C# TypeDefinition怎么用?C# TypeDefinition使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



TypeDefinition类属于命名空间,在下文中一共展示了TypeDefinition类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。

示例1: InjectAsStreamReader

    void InjectAsStreamReader(TypeDefinition targetType, FieldDefinition fieldDefinition)
    {
        AsStreamReaderMethod = new MethodDefinition("AsStreamReader", staticMethodAttributes, StreamReaderTypeReference);
        var streamVariable = new VariableDefinition(StreamTypeReference);
        AsStreamReaderMethod.Body.Variables.Add(streamVariable);
        var pathParam = new ParameterDefinition(ModuleDefinition.TypeSystem.String);
        AsStreamReaderMethod.Parameters.Add(pathParam);
        AsStreamReaderMethod.Body.InitLocals = true;
        var inst = AsStreamReaderMethod.Body.Instructions;

        var skipReturn = Instruction.Create(OpCodes.Nop);

        inst.Add(Instruction.Create(OpCodes.Ldsfld, fieldDefinition));
        inst.Add(Instruction.Create(OpCodes.Ldarg, pathParam));
        inst.Add(Instruction.Create(OpCodes.Callvirt, GetManifestResourceStreamMethod));

        inst.Add(Instruction.Create(OpCodes.Stloc, streamVariable));
        inst.Add(Instruction.Create(OpCodes.Ldloc, streamVariable));
        inst.Add(Instruction.Create(OpCodes.Brtrue_S, skipReturn));

        inst.Add(Instruction.Create(OpCodes.Ldnull));
        inst.Add(Instruction.Create(OpCodes.Ret));
        inst.Add(skipReturn);

        inst.Add(Instruction.Create(OpCodes.Ldloc, streamVariable));
        inst.Add(Instruction.Create(OpCodes.Newobj, StreamReaderConstructorReference));
        inst.Add(Instruction.Create(OpCodes.Ret));
        targetType.Methods.Add(AsStreamReaderMethod);
    }
开发者ID:huoxudong125,项目名称:Resourcer,代码行数:29,代码来源:HelperClassInjector.cs


示例2: FindGetLogger

    void FindGetLogger(TypeDefinition typeDefinition)
    {
        if (!typeDefinition.IsPublic)
        {
            var message = $"The logger factory type '{typeDefinition.FullName}' needs to be public.";
            throw new WeavingException(message);
        }
        GetLoggerMethod = typeDefinition
            .Methods
            .FirstOrDefault(x =>
                x.Name ==  "GetLogger" && 
                x.IsStatic &&
                x.HasGenericParameters &&
                x.GenericParameters.Count == 1 &&
                x.Parameters.Count == 0);

        if (GetLoggerMethod == null)
        {
            throw new WeavingException("Found 'LoggerFactory' but it did not have a static 'GetLogger' method that takes 'string' as a parameter");
        }
        if (!GetLoggerMethod.Resolve().IsPublic)
        {
            var message = $"The logger factory method '{GetLoggerMethod.FullName}' needs to be public.";
            throw new WeavingException(message);
        }
    }
开发者ID:AndreGleichner,项目名称:Anotar,代码行数:26,代码来源:LoggerFactoryFinder.cs


示例3: Execute

    public void Execute()
    {
        var validationTemplateAttribute = ModuleDefinition
            .Assembly
            .CustomAttributes
            .FirstOrDefault(x=>x.AttributeType.Name == "ValidationTemplateAttribute");

        if (validationTemplateAttribute == null)
        {
            LogInfo("Could not find a 'ValidationTemplateAttribute' on the current assembly. Going to search current assembly for 'ValidationTemplate'.");

            TypeDefinition = ModuleDefinition
                .GetTypes()
                .FirstOrDefault(x =>
                    x.Name == "ValidationTemplate" ||
                    x.Name == "ValidationTemplate`1");
            if (TypeDefinition == null)
            {
                throw new WeavingException("Could not find a type named 'ValidationTemplate'");
            }
            TypeReference = TypeDefinition;

            FindConstructor();
        }
        else
        {
            var typeReference = (TypeReference) validationTemplateAttribute.ConstructorArguments.First().Value;

            TypeReference = typeReference;
            TypeDefinition = typeReference.Resolve();

            FindConstructor();
            TemplateConstructor = ModuleDefinition.Import(TemplateConstructor);
        }
    }
开发者ID:kedarvaidya,项目名称:Validar,代码行数:35,代码来源:ValidationTemplateFinder.cs


示例4: FindIsChangedEventInvokerMethodDefinition

    bool FindIsChangedEventInvokerMethodDefinition(TypeDefinition type, out MethodDefinition methodDefinition)
    {
        //todo: check bool type
        methodDefinition = null;
        var propertyDefinition = type.Properties
            .FirstOrDefault(x =>
                            x.Name == isChangedPropertyName &&
                            x.SetMethod != null &&
                            x.SetMethod.IsPublic
            );

        if (propertyDefinition != null)
        {
            if (propertyDefinition.PropertyType.FullName != ModuleDefinition.TypeSystem.Boolean.FullName)
            {
                LogWarning(string.Format("Found '{0}' but is was of type '{1}' instead of '{2}' so it will not be used.", propertyDefinition.GetName(), propertyDefinition.PropertyType.Name, ModuleDefinition.TypeSystem.Boolean.Name));
                return false;
            }
            if (propertyDefinition.SetMethod.IsStatic)
            {
                throw new WeavingException(string.Format("Found '{0}' but is was static. Change it to non static.", propertyDefinition.GetName()));
            }
            methodDefinition = propertyDefinition.SetMethod;
        }
        return methodDefinition != null;
    }
开发者ID:jbruening,项目名称:PropertyChanged,代码行数:26,代码来源:IsChangedMethodFinder.cs


示例5: RecursiveFindMethod

    public EventInvokerMethod RecursiveFindMethod(TypeDefinition typeDefinition)
    {
        var typeDefinitions = new Stack<TypeDefinition>();
        MethodDefinition methodDefinition;
        var currentTypeDefinition = typeDefinition;
        do
        {
            typeDefinitions.Push(currentTypeDefinition);

            if (FindEventInvokerMethodDefinition(currentTypeDefinition, out methodDefinition))
            {
                break;
            }
            var baseType = currentTypeDefinition.BaseType;

            if (baseType == null || baseType.FullName == "System.Object")
            {
                return null;
            }
            currentTypeDefinition = typeResolver.Resolve(baseType);
        } while (true);

        return new EventInvokerMethod
        {
            MethodReference = methodGenerifier.GetMethodReference(typeDefinitions, methodDefinition),
            IsBeforeAfter = IsBeforeAfterMethod(methodDefinition),
        };
    }
开发者ID:shiftkey,项目名称:NotifyPropertyWeaver,代码行数:28,代码来源:MethodFinder.cs


示例6: ProcessType

    public void ProcessType(TypeDefinition typeDefinition)
    {
        if (IsCompilerGenerated(typeDefinition.CustomAttributes))
        {
            return;
        }
        if (typeDefinition.IsNotPublic)
        {
            if (typeDefinition.IsNested)
            {
                typeDefinition.IsNestedPublic = true;
            }
            else
            {
                typeDefinition.IsPublic = true;
            }
            AddEditorBrowsableAttribute(typeDefinition.CustomAttributes);
        }
        if (typeDefinition.IsInterface)
        {
            return;
        }

        foreach (var method in typeDefinition.Methods)
        {
            ProcessMethod(method);
        }
        foreach (var field in typeDefinition.Fields)
        {
            ProcessField(field);
        }
    }
开发者ID:mdabbagh88,项目名称:Publicize,代码行数:32,代码来源:TypeProcessor.cs


示例7: TryGetField

    static FieldDefinition TryGetField(TypeDefinition typeDefinition, PropertyDefinition property)
    {
        var propertyName = property.Name;
        var fieldsWithSameType = typeDefinition.Fields.Where(x => x.DeclaringType == typeDefinition).ToList();
        foreach (var field in fieldsWithSameType)
        {
            //AutoProp
            if (field.Name == $"<{propertyName}>k__BackingField")
            {
                return field;
            }
        }

        foreach (var field in fieldsWithSameType)
        {
            //diffCase
            var upperPropertyName = propertyName.ToUpper();
            var fieldUpper = field.Name.ToUpper();
            if (fieldUpper == upperPropertyName)
            {
                return field;
            }
            //underScore
            if (fieldUpper == "_" + upperPropertyName)
            {
                return field;
            }
        }
        return GetSingleField(property);
    }
开发者ID:Fody,项目名称:PropertyChanging,代码行数:30,代码来源:MappingFinder.cs


示例8: IsSecurityCritical

	public bool IsSecurityCritical (TypeDefinition type)
	{
		string entry = type.GetFullName ();
		if (critical.Contains (entry))
			return true;
		return type.IsNested ? IsSecurityCritical (type.DeclaringType) : false;
	}
开发者ID:dfr0,项目名称:moon,代码行数:7,代码来源:detect.cs


示例9: HasCorrectMethods

 private static bool HasCorrectMethods(TypeDefinition type)
 {
     return type.Methods.Any(IsOnEntryMethod) &&
            type.Methods.Any(IsOnExitMethod) &&
            type.Methods.Any(IsOnExceptionMethod) &&
            type.Methods.Any(IsOnTaskContinuationMethod);
 }
开发者ID:KonstantinFinagin,项目名称:MethodDecorator,代码行数:7,代码来源:ModuleWeaver.cs


示例10: InjectField

    void InjectField(TypeDefinition type, FieldDefinition fieldDefinition)
    {

        var staticConstructor = type.GetStaticConstructor();
        staticConstructor.Body.SimplifyMacros();
        var genericInstanceMethod = new GenericInstanceMethod(forContextDefinition);
        if (type.IsCompilerGenerated() && type.IsNested)
        {
            genericInstanceMethod.GenericArguments.Add(type.DeclaringType.GetGeneric());
        }
        else
        {
            genericInstanceMethod.GenericArguments.Add(type.GetGeneric());
        }
        var instructions = staticConstructor.Body.Instructions;
        type.Fields.Add(fieldDefinition);

        var returns = instructions.Where(_ => _.OpCode == OpCodes.Ret)
                                  .ToList();

        var ilProcessor = staticConstructor.Body.GetILProcessor();
        foreach (var returnInstruction in returns)
        {
            var newReturn = Instruction.Create(OpCodes.Ret);
            ilProcessor.InsertAfter(returnInstruction, newReturn);
            ilProcessor.InsertBefore(newReturn, Instruction.Create(OpCodes.Call, genericInstanceMethod));
            ilProcessor.InsertBefore(newReturn, Instruction.Create(OpCodes.Stsfld, fieldDefinition.GetGeneric()));
            returnInstruction.OpCode = OpCodes.Nop;
        }
        staticConstructor.Body.OptimizeMacros();
    }
开发者ID:vlaci,项目名称:Anotar,代码行数:31,代码来源:TypeProcessor.cs


示例11: InjectMethod

    MethodDefinition InjectMethod(TypeDefinition targetType, string eventInvokerName, FieldReference propertyChangingField)
    {
        var method = new MethodDefinition(eventInvokerName, GetMethodAttributes(targetType), ModuleDefinition.TypeSystem.Void);
		method.Parameters.Add(new ParameterDefinition("propertyName", ParameterAttributes.None, ModuleDefinition.TypeSystem.String));

        var handlerVariable = new VariableDefinition(PropChangingHandlerReference);
        method.Body.Variables.Add(handlerVariable);
		var boolVariable = new VariableDefinition(ModuleDefinition.TypeSystem.Boolean);
        method.Body.Variables.Add(boolVariable);

        var instructions = method.Body.Instructions;

        var last = Instruction.Create(OpCodes.Ret);
        instructions.Add(Instruction.Create(OpCodes.Ldarg_0));
        instructions.Add(Instruction.Create(OpCodes.Ldfld, propertyChangingField)); 
        instructions.Add(Instruction.Create(OpCodes.Stloc_0));
        instructions.Add(Instruction.Create(OpCodes.Ldloc_0));
        instructions.Add(Instruction.Create(OpCodes.Ldnull));
        instructions.Add(Instruction.Create(OpCodes.Ceq));
        instructions.Add(Instruction.Create(OpCodes.Stloc_1));
        instructions.Add(Instruction.Create(OpCodes.Ldloc_1));
        instructions.Add(Instruction.Create(OpCodes.Brtrue_S, last));
        instructions.Add(Instruction.Create(OpCodes.Ldloc_0));
        instructions.Add(Instruction.Create(OpCodes.Ldarg_0));
        instructions.Add(Instruction.Create(OpCodes.Ldarg_1));
        instructions.Add(Instruction.Create(OpCodes.Newobj, ComponentModelPropertyChangingEventConstructorReference));
        instructions.Add(Instruction.Create(OpCodes.Callvirt, ComponentModelPropertyChangingEventHandlerInvokeReference));


        instructions.Add(last);
        method.Body.InitLocals = true;
        targetType.Methods.Add(method);
        return method;
    }
开发者ID:pH200,项目名称:PropertyChanging.ReactiveUI.WinRT,代码行数:34,代码来源:MethodInjector.cs


示例12: InjectInterceptedMethod

    MethodDefinition InjectInterceptedMethod(TypeDefinition targetType, MethodDefinition innerOnPropertyChanging)
    {
        var delegateHolderInjector = new DelegateHolderInjector
                                     {
                                         TargetTypeDefinition=targetType,
                                         OnPropertyChangingMethodReference = innerOnPropertyChanging,
                                         ModuleWeaver = this
                                     };
        delegateHolderInjector.InjectDelegateHolder();
        var method = new MethodDefinition(EventInvokerNames.First(), GetMethodAttributes(targetType), ModuleDefinition.TypeSystem.Void);

        var propertyName = new ParameterDefinition("propertyName", ParameterAttributes.None, ModuleDefinition.TypeSystem.String);
        method.Parameters.Add(propertyName);
        if (InterceptorType == InvokerTypes.Before)
        {
            var before = new ParameterDefinition("before", ParameterAttributes.None, ModuleDefinition.TypeSystem.Object);
            method.Parameters.Add(before);
        }

        var action = new VariableDefinition("firePropertyChanging", ActionTypeReference);
        method.Body.Variables.Add(action);

        var variableDefinition = new VariableDefinition("delegateHolder", delegateHolderInjector.TypeDefinition);
        method.Body.Variables.Add(variableDefinition);

        var instructions = method.Body.Instructions;

        var last = Instruction.Create(OpCodes.Ret);
        instructions.Add(Instruction.Create(OpCodes.Newobj, delegateHolderInjector.ConstructorDefinition));
        instructions.Add(Instruction.Create(OpCodes.Stloc_1));
        instructions.Add(Instruction.Create(OpCodes.Ldloc_1));
        instructions.Add(Instruction.Create(OpCodes.Ldarg_1));
        instructions.Add(Instruction.Create(OpCodes.Stfld, delegateHolderInjector.PropertyName));
        instructions.Add(Instruction.Create(OpCodes.Ldloc_1));
        instructions.Add(Instruction.Create(OpCodes.Ldarg_0));
        instructions.Add(Instruction.Create(OpCodes.Stfld, delegateHolderInjector.Target));
        instructions.Add(Instruction.Create(OpCodes.Ldloc_1));
        instructions.Add(Instruction.Create(OpCodes.Ldftn, delegateHolderInjector.MethodDefinition));
        instructions.Add(Instruction.Create(OpCodes.Newobj, ActionConstructorReference));
        instructions.Add(Instruction.Create(OpCodes.Stloc_0));
        instructions.Add(Instruction.Create(OpCodes.Ldarg_0));
        instructions.Add(Instruction.Create(OpCodes.Ldloc_0));
        instructions.Add(Instruction.Create(OpCodes.Ldloc_1));
        instructions.Add(Instruction.Create(OpCodes.Ldfld, delegateHolderInjector.PropertyName));
        if (InterceptorType == InvokerTypes.Before)
        {
            instructions.Add(Instruction.Create(OpCodes.Ldarg_2));
            instructions.Add(Instruction.Create(OpCodes.Call, InterceptMethod));
        }
        else
        {
            instructions.Add(Instruction.Create(OpCodes.Call, InterceptMethod));
        }

        instructions.Add(last);
        method.Body.InitLocals = true;

        targetType.Methods.Add(method);
        return method;
    }
开发者ID:Fody,项目名称:PropertyChanging,代码行数:60,代码来源:MethodInjector.cs


示例13: ShouldInclude

    private static bool ShouldInclude(TypeDefinition type)
    {
        if (!type.IsClass)
        {
            return false;
        }
        if (type.IsSealed)
        {
            return false;
        }
        if (type.HasInterfaces)
        {
            return false;
        }

        if (type.IsNotPublic)
        {
            return false;
        }

        if (IsContainer(type))
        {
            return false;
        }

        return true;
    }
开发者ID:philippdolder,项目名称:Virtuosity,代码行数:27,代码来源:AssemblyProcessor.cs


示例14: GetBaseType

    internal static TypeDefinition GetBaseType(TypeDefinition child)
    {
        if (child.BaseType == null)
                return null;

            return child.BaseType.Resolve ();
    }
开发者ID:mono,项目名称:TreemapViewer,代码行数:7,代码来源:Util.cs


示例15: RecursiveFindEventInvoker

    public EventInvokerMethod RecursiveFindEventInvoker(TypeDefinition typeDefinition)
    {
        var typeDefinitions = new Stack<TypeDefinition>();
        MethodDefinition methodDefinition;
        var currentTypeDefinition = typeDefinition;
        do
        {
            typeDefinitions.Push(currentTypeDefinition);
         
            if (FindEventInvokerMethodDefinition(currentTypeDefinition, out methodDefinition))
            {
                break;
            }
            var baseType = currentTypeDefinition.BaseType;

            if (baseType == null || baseType.FullName == "System.Object")
            {
                return null;
            }
            currentTypeDefinition = Resolve(baseType);
        } while (true);

        return new EventInvokerMethod
                   {
                       MethodReference = GetMethodReference(typeDefinitions, methodDefinition),
                       IsVisibleFromChildren = IsVisibleFromChildren(methodDefinition),
                       InvokerType = ClassifyInvokerMethod(methodDefinition),
                   };
    }
开发者ID:dj-pgs,项目名称:PropertyChanged,代码行数:29,代码来源:MethodFinder.cs


示例16: AddOnPropertyChangedMethod

    public EventInvokerMethod AddOnPropertyChangedMethod(TypeDefinition targetType)
    {
        var propertyChangedField = FindPropertyChangedField(targetType);
        if (propertyChangedField == null)
        {
            return null;
        }

        if (FoundInterceptor)
        {
            if (targetType.HasGenericParameters)
            {
                var message = string.Format("Error processing '{0}'. Interception is not supported on generic types. To manually work around this problem add a [DoNotNotify] to the class and then manually implement INotifyPropertyChanged for that class and all child classes. If you would like this feature handled automatically please feel free to submit a pull request.", targetType.Name);
                throw new WeavingException(message);
            }
            var methodDefinition = GetMethodDefinition(targetType, propertyChangedField);

            return new EventInvokerMethod
                       {
                           MethodReference = InjectInterceptedMethod(targetType, methodDefinition).GetGeneric(),
                           InvokerType = InterceptorType,
                           IsVisibleFromChildren = true,
                       };
        }
        return new EventInvokerMethod
                   {
                       MethodReference = InjectMethod(targetType, EventInvokerNames.First(), propertyChangedField).GetGeneric(),
                       InvokerType = InterceptorType,
                       IsVisibleFromChildren = true,
                   };
    }
开发者ID:jbruening,项目名称:PropertyChanged,代码行数:31,代码来源:MethodInjector.cs


示例17: CreateFields

 void CreateFields(TypeDefinition targetTypeDefinition)
 {
     TargetField = new FieldDefinition("target", FieldAttributes.Public, targetTypeDefinition);
     TypeDefinition.Fields.Add(TargetField);
     PropertyNameField = new FieldDefinition("propertyName", FieldAttributes.Public, ModuleWeaver.ModuleDefinition.TypeSystem.String);
     TypeDefinition.Fields.Add(PropertyNameField);
 }
开发者ID:dj-pgs,项目名称:PropertyChanged,代码行数:7,代码来源:DelegateHolderInjector.cs


示例18: CreateDefaultConstructor

    /// <summary>
    /// Creates the default constructor.
    /// </summary>
    public MethodDefinition CreateDefaultConstructor(TypeDefinition type)
    {
        _logInfo("AddDefaultConstructor() " + type.Name);

        // Create method for a constructor
        var methodAttributes = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName;
        var method = new MethodDefinition(".ctor", methodAttributes, _moduleDefinition.TypeSystem.Void);

        // Set parameters for constructor, and add corresponding assignment in method body:
        // 1. Add each property in the class as a parameter for the constructor
        // 2. Set the properties in the class from the constructor parameters
        // Properties marked with the [ImmutableClassIgnore] attribute are ignored
        foreach (var prop in type.Properties)
        {
            if (prop.CustomAttributes.ContainsAttribute("ImmutableClassIgnoreAttribute"))
            {
                _logInfo("Ignoring property " + prop.Name);
                continue;
            }
            string paramName = Char.ToLowerInvariant(prop.Name [0]) + prop.Name.Substring (1); // convert first letter of name to lowercase
            _logInfo("Adding parameter " + paramName + " to ctor");
            var pd = (new ParameterDefinition(paramName, ParameterAttributes.HasDefault, prop.PropertyType));
            method.Parameters.Add(pd);
            _logInfo("Setting property " + prop.Name);
            method.Body.Instructions.Add(Instruction.Create(OpCodes.Ldarg_0));
            method.Body.Instructions.Add(Instruction.Create(OpCodes.Ldarg, pd));
            method.Body.Instructions.Add(Instruction.Create(OpCodes.Call, prop.SetMethod));
        }

        method.Body.Instructions.Add(Instruction.Create(OpCodes.Ret));
        return method;
    }
开发者ID:taiste,项目名称:ImmutableClass.Fody,代码行数:35,代码来源:MethodCreator.cs


示例19: ProcessType

 void ProcessType(TypeDefinition typeDefinition)
 {
     foreach (var method in typeDefinition.Methods)
     {
         ProcessMethod(method);
     }
 }
开发者ID:Bebere,项目名称:Tail.Fody,代码行数:7,代码来源:OptimizeRecursion.cs


示例20: NullableTypeDefinition

        private NullableTypeDefinition(Type type, Type underlyingType)
            : base(type)
        {
            if (underlyingType == null) throw new ArgumentNullException("underlyingType");

            underlyingTypeDef = TypeCache.GetTypeDefinition(underlyingType);
        }
开发者ID:ZapTechnology,项目名称:ForSerial,代码行数:7,代码来源:NullableTypeDefinition.cs



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C# TypeDefinitionHandle类代码示例发布时间:2022-05-24
下一篇:
C# TypeDef类代码示例发布时间:2022-05-24
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap