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


C# TypeDefinition.GetConstructors方法代码示例

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


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

示例1: WeaveType

    private void WeaveType(TypeDefinition type, Dictionary<string, Tuple<MethodReference, MethodReference>> methodTable)
    {
        Debug.WriteLine("Weaving " + type.Name);

        var persistedProperties = new List<WeaveResult>();
        foreach (var prop in type.Properties.Where(x => x.HasThis && !x.CustomAttributes.Any(a => a.AttributeType.Name == "IgnoredAttribute")))
        {
            try
            {
                var weaveResult = WeaveProperty(prop, type, methodTable);
                if (weaveResult.Woven)
                {
                    persistedProperties.Add(weaveResult);
                }
                else
                {
                    var sequencePoint = prop.GetMethod.Body.Instructions.First().SequencePoint;
                    if (!string.IsNullOrEmpty(weaveResult.ErrorMessage))
                    {
                        // We only want one error point, so even though there may be more problems, we only log the first one.
                        LogErrorPoint(weaveResult.ErrorMessage, sequencePoint);
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(weaveResult.WarningMessage))
                        {
                            LogWarningPoint(weaveResult.WarningMessage, sequencePoint);
                        }

                        var realmAttributeNames = prop.CustomAttributes
                                                      .Select(a => a.AttributeType.Name)
                                                      .Intersect(RealmPropertyAttributes)
                                                      .Select(a => $"[{a.Replace("Attribute", string.Empty)}]");
                        
                        if (realmAttributeNames.Any())
                        {
                            LogErrorPoint($"{type.Name}.{prop.Name} has {string.Join(", ", realmAttributeNames)} applied, but it's not persisted, so those attributes will be ignored.", sequencePoint);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                var sequencePoint = prop.GetMethod.Body.Instructions.First().SequencePoint;
                LogErrorPoint(
                    $"Unexpected error caught weaving property '{type.Name}.{prop.Name}': {e.Message}.\r\nCallstack:\r\n{e.StackTrace}",
                    sequencePoint);
            }
        }

        if (!persistedProperties.Any())
        {
            LogError($"Class {type.Name} is a RealmObject but has no persisted properties.");
            return;
        }

        if (persistedProperties.Count(p => p.IsPrimaryKey) > 1)
        {
            LogError($"Class {type.Name} has more than one property marked with [PrimaryKey].");
            return;
        }

        var objectConstructor = type.GetConstructors()
            .SingleOrDefault(c => c.Parameters.Count == 0 && !c.IsStatic);
        if (objectConstructor == null)
        {
            var nonDefaultConstructor = type.GetConstructors().First();
            var sequencePoint = nonDefaultConstructor.Body.Instructions.First().SequencePoint;
            LogErrorPoint($"Class {type.Name} must have a public constructor that takes no parameters.", sequencePoint);
            return;
        }

        var preserveAttribute = new CustomAttribute(_references.PreserveAttribute_Constructor);
        objectConstructor.CustomAttributes.Add(preserveAttribute);
        preserveAttribute = new CustomAttribute(_references.PreserveAttribute_ConstructorWithParams); // recreate so has new instance
        preserveAttribute.ConstructorArguments.Add(new CustomAttributeArgument(ModuleDefinition.TypeSystem.Boolean, true)); // AllMembers
        preserveAttribute.ConstructorArguments.Add(new CustomAttributeArgument(ModuleDefinition.TypeSystem.Boolean, false)); // Conditional
        type.CustomAttributes.Add(preserveAttribute);
        LogDebug($"Added [Preserve] to {type.Name} and its constructor.");

        var wovenAttribute = new CustomAttribute(_references.WovenAttribute_Constructor);
        TypeReference helperType = WeaveRealmObjectHelper(type, objectConstructor, persistedProperties);
        wovenAttribute.ConstructorArguments.Add(new CustomAttributeArgument(_references.System_Type, helperType));
        type.CustomAttributes.Add(wovenAttribute);
        Debug.WriteLine(string.Empty);
    }
开发者ID:realm,项目名称:realm-dotnet,代码行数:86,代码来源:ModuleWeaver.cs

示例2: CreateParameterlessConstructor

    private MethodDefinition CreateParameterlessConstructor(TypeDefinition type, MethodReference parentConstructor)
    {
        var constructor = new MethodDefinition(".ctor", MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName, TypeSystem.Void);
          var processor = constructor.Body.GetILProcessor();

          // Select referencedConstructor whose field initializer code we're cloning
          // Note: Field initializers aren't optimized away, even if the field is overwritten by ctor!
          var referencedConstructor = type.GetConstructors().FirstOrDefault();
          if (referencedConstructor != null) {
         // Copy all instructions to last field initializer set-field.
         // If we don't run into a .ctor somehow, all instructions are copied.
         var referencedBody = referencedConstructor.Body;
         var referencedInstructions = referencedBody.Instructions;
         int copiedInstructionCount = referencedInstructions.Count;
         var superCall = referencedInstructions.FirstOrDefault(
            i => i.OpCode == OpCodes.Call && ((MethodReference)i.Operand).Name == ".ctor");
         if (superCall != null) {
            copiedInstructionCount = referencedInstructions.IndexOf(superCall);
            while (copiedInstructionCount > 0 &&
                   referencedInstructions[copiedInstructionCount - 1].OpCode != OpCodes.Stfld) {
               copiedInstructionCount--;
            }
         }
         for (var i = 0; i < copiedInstructionCount; i++) {
            processor.Append(referencedInstructions[i]);
         }
          }

          // Call parent constructor
          processor.Emit(OpCodes.Ldarg_0);
          processor.Emit(OpCodes.Call, parentConstructor);
          processor.Emit(OpCodes.Ret);
          return constructor;
    }
开发者ID:ItzWarty,项目名称:Constructors,代码行数:34,代码来源:ModuleWeaver.cs


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