本文整理汇总了C#中System.Reflection.Emit.TypeBuilder.DefineTypeInitializer方法的典型用法代码示例。如果您正苦于以下问题:C# TypeBuilder.DefineTypeInitializer方法的具体用法?C# TypeBuilder.DefineTypeInitializer怎么用?C# TypeBuilder.DefineTypeInitializer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Reflection.Emit.TypeBuilder
的用法示例。
在下文中一共展示了TypeBuilder.DefineTypeInitializer方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ILEmitter
/// <summary>
/// Ensure the call sites container is created and return the <see cref="TypeBuilder"/>.
/// </summary>
/// <returns></returns>
private TypeBuilder/*!*/EnsureContainer()
{
if (containerClass == null)
{
if (this.classContext != null && this.classContext.IsGeneric)
{
// we will emit single call sites in the class context. It is easier than to build generic sites container.
Debug.Assert(this.classContext.RealTypeBuilder != null);
return this.classContext.RealTypeBuilder;
}
Debug.Assert(staticCtorEmitter == null);
var containerClassName = string.Format("<{0}>o_Sitescontainer'{1}", this.userFriendlyName.Replace('.', '_'), System.Threading.Interlocked.Increment(ref nextContainerId));
containerClass = moduleBuilder.DefineType(PluginHandler.ConvertCallSiteName(containerClassName), TypeAttributes.Sealed | TypeAttributes.Class | TypeAttributes.NotPublic | TypeAttributes.Abstract);
staticCtorEmitter = new ILEmitter(containerClass.DefineTypeInitializer());
}
return containerClass;
}
示例2: EmitConstructorsAndProperties
// Emits a constructor that takes an object that the surrogate wraps, as well as
// a type initializer that will load (for this AppDomain) all the PropertyDescriptors
// for virtual properties.
private static void EmitConstructorsAndProperties(TypeBuilder typeBuilder, Type type, Type parentSurrogateType, Type parentType, FieldInfo wrapperField)
{
// public Surrogate(Entity entity) : base(entity)
ConstructorBuilder constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new Type[] { type });
ILGenerator constructorGenerator = constructorBuilder.GetILGenerator();
if (parentSurrogateType == typeof(object))
{
// base()
constructorGenerator.Emit(OpCodes.Ldarg_0);
constructorGenerator.Emit(OpCodes.Call, parentSurrogateType.GetConstructor(Type.EmptyTypes));
// _wrapper = wrapper
constructorGenerator.Emit(OpCodes.Ldarg_0);
constructorGenerator.Emit(OpCodes.Ldarg_1);
constructorGenerator.Emit(OpCodes.Stfld, wrapperField);
}
else
{
// base(entity)
constructorGenerator.Emit(OpCodes.Ldarg_0);
constructorGenerator.Emit(OpCodes.Ldarg_1);
constructorGenerator.Emit(OpCodes.Call, parentSurrogateType.GetConstructor(new Type[] { parentType }));
}
constructorGenerator.Emit(OpCodes.Ret);
// Only generate a type initializer if there are any virtual properties.
Lazy<ILGenerator> typeInitializerFactory = new Lazy<ILGenerator>(() =>
{
// static Surrogate() {
// PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(Entity));
// (_Property = properties["Property"];)*
// }
ConstructorBuilder typeInitializerBuilder = typeBuilder.DefineTypeInitializer();
ILGenerator typeInitializerGenerator = typeInitializerBuilder.GetILGenerator();
// PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(Entity));
typeInitializerGenerator.DeclareLocal(typeof(PropertyDescriptorCollection));
typeInitializerGenerator.Emit(OpCodes.Ldtoken, type);
typeInitializerGenerator.Emit(OpCodes.Call, typeof(Type).GetMethod("GetTypeFromHandle"));
typeInitializerGenerator.Emit(OpCodes.Call, typeof(TypeDescriptor).GetMethod("GetProperties", new Type[] { typeof(Type) }));
typeInitializerGenerator.Emit(OpCodes.Stloc_0);
return typeInitializerGenerator;
});
// Generate all the properties from here, because properties potentially add code to the type initializer.
EmitProperties(typeBuilder, type, parentSurrogateType, wrapperField, typeInitializerFactory);
if (typeInitializerFactory.IsValueCreated)
{
typeInitializerFactory.Value.Emit(OpCodes.Ret);
}
}
示例3: OnPropertyGenerationComplete
protected override void OnPropertyGenerationComplete(TypeDefinition proxyType)
{
const string initPropertyWrappersMethodName = "InitPropertyWrappers";
var initPropertyWrappersMethod = proxyType.DefineMethod(
initPropertyWrappersMethodName,
MethodAttributes.Static | MethodAttributes.Private,
null, Type.EmptyTypes);
var il = initPropertyWrappersMethod.GetILGenerator();
this.initPropertyWrapperIlAction.ForEach(m => m(il));
il.Emit(OpCodes.Ret);
var cctor = proxyType.DefineTypeInitializer();
var cctorIl = cctor.GetILGenerator();
cctorIl.Emit(OpCodes.Call, initPropertyWrappersMethod);
cctorIl.Emit(OpCodes.Ret);
}
示例4: CreateStaticConstructor
private void CreateStaticConstructor(TypeBuilder typeBuilder, RppField instanceField, RppClass obj)
{
FieldInfo instanceFieldInfo = instanceField.FieldInfo.Native;
ConstructorBuilder staticConstructor = typeBuilder.DefineTypeInitializer();
_body = staticConstructor.GetILGenerator();
instanceField.InitExpr.Accept(this);
_body.Emit(OpCodes.Stsfld, instanceFieldInfo);
_body.Emit(OpCodes.Ret);
}
示例5: DeserializeCore
private Type DeserializeCore(TypeBuilder tb)
{
m_currentTb = tb;
// TODO custom attr for tb
ReadCustomAttributes(tb);
foreach (var t in ReadTypeArray())
tb.AddInterfaceImplementation(t);
int num = m_br.ReadInt32();
for (int i = 0; i < num; i++)
{
var fb = tb.DefineField(
m_br.ReadString(),
ReadType(),
(FieldAttributes)m_br.ReadUInt32());
// TODO custom attr for fb
//Console.WriteLine($"Read => {fb.Name} = {fb.MetadataToken:X8}");
m_fields.Add(m_br.ReadString(), fb);
}
num = m_br.ReadInt32();
for (int i = 0; i < num; i++)
ReadMethod();
// read props
num = m_br.ReadInt32();
for (int i = 0; i < num; i++)
{
var pb = tb.DefineProperty(
m_br.ReadString(),
(PropertyAttributes)m_br.ReadUInt32(),
ReadType(),
ReadTypeArray());
// TODO custom attr for pb
if (m_br.ReadBoolean())
{
// default value
}
if (m_br.ReadBoolean()) // canread
pb.SetGetMethod(ReadMethod());
if (m_br.ReadBoolean()) // canwrite
pb.SetSetMethod(ReadMethod());
}
num = m_br.ReadInt32();
for (int i = 0; i < num; i++)
{
var cb = tb.DefineConstructor(
(MethodAttributes)m_br.ReadUInt32(),
(CallingConventions)m_br.ReadUInt32(),
ReadTypeArray());
ReadConstructor(cb);
}
if (m_br.ReadBoolean())
{
var ti = tb.DefineTypeInitializer();
ReadConstructor(ti, true);
}
num = m_br.ReadInt32();
for (int i = 0; i < num; i++)
{
var mb = ResolveMethod(m_br.ReadString());
if (!mb.IsAbstract)
ReadMethodBody(mb);
}
num = m_br.ReadInt32();
for (int i = 0; i < num; i++)
DeserializeNested(tb);
return tb.CreateType();
}