本文整理汇总了C#中Mono.Cecil.TypeDefinition.AddDefaultConstructor方法的典型用法代码示例。如果您正苦于以下问题:C# TypeDefinition.AddDefaultConstructor方法的具体用法?C# TypeDefinition.AddDefaultConstructor怎么用?C# TypeDefinition.AddDefaultConstructor使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Mono.Cecil.TypeDefinition
的用法示例。
在下文中一共展示了TypeDefinition.AddDefaultConstructor方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AddConstructor
public void AddConstructor(TypeDefinition adapterType,
FieldReference targetField)
{
var adapterCtor = adapterType.AddDefaultConstructor();
var adapterParameter = new ParameterDefinition(_targetDependency);
adapterCtor.Parameters.Add(adapterParameter);
// HACK: Remove the ret instruction from the default constructor and replace it with
// the field setter
var adapterBody = adapterCtor.Body;
var adapterInstructions = adapterBody.Instructions.Cast<Instruction>().Where(i => i.OpCode != OpCodes.Ret).ToArray();
adapterBody.Instructions.Clear();
// Copy the old instructions
var IL = adapterBody.CilWorker;
foreach (var instruction in adapterInstructions)
{
IL.Append(instruction);
}
IL.Emit(OpCodes.Ldarg_0);
IL.Emit(OpCodes.Ldarg, adapterParameter);
IL.Emit(OpCodes.Stfld, targetField);
IL.Emit(OpCodes.Ret);
}
示例2: DefineSerializationConstructor
private static void DefineSerializationConstructor(ModuleDefinition module, TypeDefinition targetType)
{
var getTypeFromHandle = module.ImportMethod<Type>("GetTypeFromHandle",
BindingFlags.Public | BindingFlags.Static);
var parameterTypes = new[] {typeof (SerializationInfo), typeof (StreamingContext)};
// Define the constructor signature
var serializationCtor = targetType.AddDefaultConstructor();
serializationCtor.AddParameters(parameterTypes);
serializationCtor.Attributes = MethodAttributes.HideBySig | MethodAttributes.SpecialName |
MethodAttributes.RTSpecialName | MethodAttributes.Public;
var interceptorInterfaceType = module.ImportType<IInterceptor>();
var interceptorTypeVariable = serializationCtor.AddLocal<Type>();
var body = serializationCtor.Body;
body.InitLocals = true;
var IL = serializationCtor.GetILGenerator();
IL.Emit(OpCodes.Ldtoken, interceptorInterfaceType);
IL.Emit(OpCodes.Call, getTypeFromHandle);
IL.Emit(OpCodes.Stloc, interceptorTypeVariable);
var defaultConstructor = module.ImportConstructor<object>();
IL.Emit(OpCodes.Ldarg_0);
IL.Emit(OpCodes.Call, defaultConstructor);
// __interceptor = (IInterceptor)info.GetValue("__interceptor", typeof(IInterceptor));
var getValue = module.ImportMethod<SerializationInfo>("GetValue");
IL.Emit(OpCodes.Ldarg_0);
IL.Emit(OpCodes.Ldarg_1);
IL.Emit(OpCodes.Ldstr, "__interceptor");
IL.Emit(OpCodes.Ldloc, interceptorTypeVariable);
IL.Emit(OpCodes.Callvirt, getValue);
IL.Emit(OpCodes.Castclass, interceptorInterfaceType);
var setInterceptor = module.ImportMethod<IProxy>("set_Interceptor");
IL.Emit(OpCodes.Callvirt, setInterceptor);
;
IL.Emit(OpCodes.Ret);
}
示例3: SetDataTemplate
static void SetDataTemplate(IElementNode parentNode, ElementNode node, ILContext parentContext,
IXmlLineInfo xmlLineInfo)
{
var parentVar = parentContext.Variables[parentNode];
//Push the DataTemplate to the stack, for setting the template
parentContext.IL.Emit(OpCodes.Ldloc, parentVar);
//Create nested class
// .class nested private auto ansi sealed beforefieldinit '<Main>c__AnonStorey0'
// extends [mscorlib]System.Object
var module = parentContext.Body.Method.Module;
var anonType = new TypeDefinition(
null,
"<" + parentContext.Body.Method.Name + ">_anonXamlCDataTemplate_" + dtcount++,
TypeAttributes.BeforeFieldInit |
TypeAttributes.Sealed |
TypeAttributes.NestedPrivate)
{
BaseType = module.TypeSystem.Object
};
parentContext.Body.Method.DeclaringType.NestedTypes.Add(anonType);
var ctor = anonType.AddDefaultConstructor();
var loadTemplate = new MethodDefinition("LoadDataTemplate",
MethodAttributes.Assembly | MethodAttributes.HideBySig,
module.TypeSystem.Object);
anonType.Methods.Add(loadTemplate);
var parentValues = new FieldDefinition("parentValues", FieldAttributes.Assembly, module.Import(typeof (object[])));
anonType.Fields.Add(parentValues);
TypeReference rootType = null;
var vdefRoot = parentContext.Root as VariableDefinition;
if (vdefRoot != null)
rootType = vdefRoot.VariableType;
var fdefRoot = parentContext.Root as FieldDefinition;
if (fdefRoot != null)
rootType = fdefRoot.FieldType;
var root = new FieldDefinition("root", FieldAttributes.Assembly, rootType);
anonType.Fields.Add(root);
//Fill the loadTemplate Body
var templateIl = loadTemplate.Body.GetILProcessor();
templateIl.Emit(OpCodes.Nop);
var templateContext = new ILContext(templateIl, loadTemplate.Body, parentValues)
{
Root = root
};
node.Accept(new CreateObjectVisitor(templateContext), null);
node.Accept(new SetNamescopesAndRegisterNamesVisitor(templateContext), null);
node.Accept(new SetFieldVisitor(templateContext), null);
node.Accept(new SetResourcesVisitor(templateContext), null);
node.Accept(new SetPropertiesVisitor(templateContext), null);
templateIl.Emit(OpCodes.Ldloc, templateContext.Variables[node]);
templateIl.Emit(OpCodes.Ret);
//Instanciate nested class
var parentIl = parentContext.IL;
parentIl.Emit(OpCodes.Newobj, ctor);
//Copy required local vars
parentIl.Emit(OpCodes.Dup); //Duplicate the nestedclass instance
parentIl.Append(node.PushParentObjectsArray(parentContext));
parentIl.Emit(OpCodes.Stfld, parentValues);
parentIl.Emit(OpCodes.Dup); //Duplicate the nestedclass instance
if (parentContext.Root is VariableDefinition)
parentIl.Emit(OpCodes.Ldloc, parentContext.Root as VariableDefinition);
else if (parentContext.Root is FieldDefinition)
{
parentIl.Emit(OpCodes.Ldarg_0);
parentIl.Emit(OpCodes.Ldfld, parentContext.Root as FieldDefinition);
}
else
throw new InvalidProgramException();
parentIl.Emit(OpCodes.Stfld, root);
//SetDataTemplate
parentIl.Emit(OpCodes.Ldftn, loadTemplate);
var funcCtor =
module.Import(typeof (Func<>))
.MakeGenericInstanceType(module.TypeSystem.Object)
.Resolve()
.Methods.First(md => md.IsConstructor && md.Parameters.Count == 2)
.MakeGeneric(module.TypeSystem.Object);
parentIl.Emit(OpCodes.Newobj, module.Import(funcCtor));
var propertySetter =
module.Import(typeof (IDataTemplate)).Resolve().Properties.First(p => p.Name == "LoadTemplate").SetMethod;
parentContext.IL.Emit(OpCodes.Callvirt, module.Import(propertySetter));
}
示例4: DefineNestedConstructors
/// <summary>
/// Defines the nested constructors for the singleton type.
/// </summary>
/// <param name="module">The target module.</param>
/// <param name="nestedType">The nested type.</param>
/// <returns>The static singleton constructor.</returns>
private static MethodDefinition DefineNestedConstructors(ModuleDefinition module, TypeDefinition nestedType)
{
// Define the constructor for the nested t ype
nestedType.AddDefaultConstructor();
var defaultConstructor = nestedType.GetDefaultConstructor();
defaultConstructor.IsPublic = true;
var cctor = DefineStaticConstructor(module, nestedType);
cctor.IsPublic = true;
return cctor;
}