本文整理汇总了C#中ILGen.EmitFieldSet方法的典型用法代码示例。如果您正苦于以下问题:C# ILGen.EmitFieldSet方法的具体用法?C# ILGen.EmitFieldSet怎么用?C# ILGen.EmitFieldSet使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ILGen
的用法示例。
在下文中一共展示了ILGen.EmitFieldSet方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Create
internal static Type Create(GenContext context, Type baseClass)
{
//ModuleBuilder mb = context.ModuleBldr;
string name = baseClass.Name + "_impl";
//TypeBuilder baseTB = context.ModuleBldr.DefineType(name, TypeAttributes.Class | TypeAttributes.Public, baseClass);
TypeBuilder baseTB = context.AssemblyGen.DefinePublicType(name, baseClass, true);
ObjExpr.MarkAsSerializable(baseTB);
baseTB.DefineDefaultConstructor(MethodAttributes.Public);
FieldBuilder metaField = baseTB.DefineField("_meta", typeof(IPersistentMap), FieldAttributes.Public);
MethodBuilder metaMB = baseTB.DefineMethod("meta", MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.ReuseSlot, typeof(IPersistentMap), Type.EmptyTypes);
ILGen gen = new ILGen(metaMB.GetILGenerator());
gen.EmitLoadArg(0);
gen.EmitFieldGet(metaField);
gen.Emit(OpCodes.Ret);
MethodBuilder withMB = baseTB.DefineMethod("withMeta", MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.ReuseSlot, typeof(IObj), new Type[] { typeof(IPersistentMap)});
gen = new ILGen(withMB.GetILGenerator());
gen.EmitLoadArg(0);
gen.EmitCall(Compiler.Method_Object_MemberwiseClone);
gen.Emit(OpCodes.Castclass, baseTB);
gen.Emit(OpCodes.Dup);
gen.EmitLoadArg(1);
gen.EmitFieldSet(metaField);
gen.Emit(OpCodes.Ret);
for (int i = 0; i < 20; i++ )
DefineDelegateFieldAndOverride(baseTB, i);
return baseTB.CreateType();
}
示例2: BuildConstructors
private void BuildConstructors(IList<ConstructorBuilderInfo>/*!*/ ctors) {
foreach (var ctor in ctors) {
// ctor(... RubyClass! class ..., <visible params>) : base(<hidden params>, <visible params>) { _class = class; }
// ctor(... RubyClass! class ..., <visible params>) : base(... RubyOps.GetContextFromClass(class) ..., <visible params>) { _class = class; }
// ctor(RubyClass! class) : base(RubyOps.GetDefaultExceptionMessage(class)) { _class = class; }
ConstructorBuilder cb = _tb.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, ctor.ParameterTypes);
ILGen il = new ILGen(cb.GetILGenerator());
int paramIndex = 0;
int argIndex = 0;
// We need to initialize before calling base ctor since the ctor can call virtual methods.
// _immediateClass = immediateClass:
if (!IsDerivedRubyType) {
il.EmitLoadArg(0);
il.EmitLoadArg(1 + ctor.ClassParamIndex);
il.EmitFieldSet(ImmediateClassField);
}
// base ctor call:
il.EmitLoadArg(0);
ConstructorInfo msgCtor;
if (ctor.ParameterTypes.Length == 1 && ctor.Adjustment == SignatureAdjustment.InsertClass &&
_tb.IsSubclassOf(typeof(Exception)) && IsAvailable(msgCtor = _tb.BaseType.GetConstructor(_exceptionMessageSignature))) {
// a parameterless exception constructor should use Ruby default message:
il.EmitLoadArg(1);
il.EmitCall(Methods.GetDefaultExceptionMessage);
il.Emit(OpCodes.Call, msgCtor);
} else {
if (ctor.Adjustment == SignatureAdjustment.InsertClass) {
paramIndex++;
}
while (paramIndex < ctor.ParameterTypes.Length) {
if (ctor.Adjustment == SignatureAdjustment.ConvertClassToContext && argIndex == ctor.ContextArgIndex) {
il.EmitLoadArg(1 + ctor.ClassParamIndex);
il.EmitCall(Methods.GetContextFromModule);
} else {
ClsTypeEmitter.DefineParameterCopy(cb, paramIndex, ctor.BaseParameters[argIndex]);
il.EmitLoadArg(1 + paramIndex);
}
argIndex++;
paramIndex++;
}
il.Emit(OpCodes.Call, ctor.BaseCtor);
}
il.Emit(OpCodes.Ret);
}
}
示例3: EmitExposers
private static void EmitExposers(TypeBuilder proxyTB, Type superClass, IPersistentMap exposesFields)
{
for ( ISeq s = RT.seq(exposesFields); s != null; s = s.next() )
{
IMapEntry me = (IMapEntry)s.first();
Symbol protectedFieldSym = (Symbol)me.key();
IPersistentMap accessMap = (IPersistentMap)me.val();
string fieldName = protectedFieldSym.Name;
Symbol getterSym = (Symbol)accessMap.valAt(_getKw, null);
Symbol setterSym = (Symbol)accessMap.valAt(_setKW, null);
FieldInfo fld = null;
if ( getterSym != null || setterSym != null )
fld = superClass.GetField(fieldName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy | BindingFlags.Static | BindingFlags.Instance);
if (getterSym != null)
{
MethodAttributes attribs = MethodAttributes.Public;
if (fld.IsStatic)
attribs |= MethodAttributes.Static;
MethodBuilder mb = proxyTB.DefineMethod(getterSym.Name, attribs, fld.FieldType, Type.EmptyTypes);
ILGen gen = new ILGen(mb.GetILGenerator());
//if (fld.IsStatic)
// gen.Emit(OpCodes.Ldsfld, fld);
//else
//{
// gen.Emit(OpCodes.Ldarg_0);
// gen.Emit(OpCodes.Ldfld, fld);
//}
if (!fld.IsStatic)
gen.EmitLoadArg(0);
gen.EmitFieldGet(fld);
gen.Emit(OpCodes.Ret);
}
if (setterSym != null)
{
MethodAttributes attribs = MethodAttributes.Public;
if (fld.IsStatic)
attribs |= MethodAttributes.Static;
MethodBuilder mb = proxyTB.DefineMethod(setterSym.Name, attribs, typeof(void), new Type[] { fld.FieldType });
ILGen gen = new ILGen(mb.GetILGenerator());
if (fld.IsStatic)
{
gen.Emit(OpCodes.Ldarg_0);
//gen.Emit(OpCodes.Stsfld, fld);
}
else
{
gen.Emit(OpCodes.Ldarg_0);
gen.Emit(OpCodes.Ldarg_1);
//gen.Emit(OpCodes.Stfld, fld);
}
gen.EmitFieldSet(fld);
gen.Emit(OpCodes.Ret);
}
}
}
示例4: DefineCtors
static void DefineCtors(TypeBuilder proxyTB,
Type superClass,
string initName,
string postInitName,
ISeq ctorsTypes,
FieldBuilder initFB,
FieldBuilder postInitFB,
FieldBuilder stateFB,
string factoryName)
{
for (ISeq s = ctorsTypes; s != null; s = s.next())
{
IMapEntry me = (IMapEntry)s.first();
ISeq thisParamTypesV = (ISeq)me.key();
ISeq baseParamTypesV = (ISeq)me.val();
Type[] thisParamTypes = CreateTypeArray(thisParamTypesV);
Type[] baseParamTypes = CreateTypeArray(baseParamTypesV);
BindingFlags flags = BindingFlags.CreateInstance| BindingFlags.NonPublic| BindingFlags.Public| BindingFlags.Instance;
ConstructorInfo superCtor = superClass.GetConstructor(flags,null,baseParamTypes,null);
if (superCtor == null || superCtor.IsPrivate)
throw new InvalidOperationException("Base class constructor missing or private");
ConstructorBuilder cb = proxyTB.DefineConstructor(MethodAttributes.Public, CallingConventions.HasThis, thisParamTypes);
ILGen gen = new ILGen(cb.GetILGenerator());
Label noInitLabel = gen.DefineLabel();
Label noPostInitLabel = gen.DefineLabel();
Label endPostInitLabel = gen.DefineLabel();
Label endLabel = gen.DefineLabel();
LocalBuilder locSuperArgs = gen.DeclareLocal(typeof(object));
LocalBuilder locInitVal = gen.DeclareLocal(typeof(object));
if (initFB != null)
{
// init supplied
EmitGetVar(gen, initFB);
gen.Emit(OpCodes.Dup);
gen.Emit(OpCodes.Brfalse_S, noInitLabel);
gen.Emit(OpCodes.Castclass, typeof(IFn));
// box init args
for (int i = 0; i < thisParamTypes.Length; i++)
{
gen.EmitLoadArg(i + 1); // gen.Emit(OpCodes.Ldarg, i + 1);
if (thisParamTypes[i].IsValueType)
gen.Emit(OpCodes.Box,thisParamTypes[i]);
}
gen.EmitCall(Compiler.Methods_IFn_invoke[thisParamTypes.Length]); // gen.Emit(OpCodes.Call, Compiler.Methods_IFn_invoke[thisParamTypes.Length]);
// Expecting: [[super-ctor-args...] state]
// store the init return in a local
gen.Emit(OpCodes.Dup);
gen.Emit(OpCodes.Stloc,locInitVal);
// store the first element in a local
gen.EmitInt(0); // gen.Emit(OpCodes.Ldc_I4_0);
gen.EmitCall(Method_RT_nth); // gen.Emit(OpCodes.Call, Method_RT_nth);
gen.Emit(OpCodes.Stloc, locSuperArgs);
// Stack this + super-ctor-args + call base-class ctor.
gen.EmitLoadArg(0); // gen.Emit(OpCodes.Ldarg_0);
for (int i = 0; i < baseParamTypes.Length; i++)
{
gen.Emit(OpCodes.Ldloc, locSuperArgs);
gen.EmitInt(i); // gen.Emit(OpCodes.Ldc_I4, i);
gen.EmitCall(Method_RT_nth); // gen.Emit(OpCodes.Call, Method_RT_nth);
if (baseParamTypes[i].IsValueType)
gen.Emit(OpCodes.Unbox_Any, baseParamTypes[i]);
else
gen.Emit(OpCodes.Castclass, baseParamTypes[i]);
}
gen.Emit(OpCodes.Call, superCtor);
if (stateFB != null)
{
gen.EmitLoadArg(0); // gen.Emit(OpCodes.Ldarg_0);
gen.Emit(OpCodes.Ldloc, locInitVal);
gen.EmitInt(1); // gen.Emit(OpCodes.Ldc_I4_1);
gen.EmitCall(Method_RT_nth); // gen.Emit(OpCodes.Call, Method_RT_nth);
gen.Emit(OpCodes.Castclass, typeof(object));
gen.EmitFieldSet(stateFB); // gen.Emit(OpCodes.Stfld, stateFB);
}
gen.Emit(OpCodes.Br_S, endLabel);
// No init found
gen.MarkLabel(noInitLabel);
gen.Emit(OpCodes.Pop);
EmitUnsupported(gen, initName);
gen.MarkLabel(endLabel);
}
//.........这里部分代码省略.........
示例5: ImplementProtectedFieldAccessors
private void ImplementProtectedFieldAccessors(Dictionary<string, string[]> specialNames) {
// For protected fields to be accessible from the derived type in Silverlight,
// we need to create public helper methods that expose them. These methods are
// used by the IDynamicMetaObjectProvider implementation (in MetaUserObject)
FieldInfo[] fields = _baseType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.FlattenHierarchy);
foreach (FieldInfo fi in fields) {
if (!fi.IsProtected()) {
continue;
}
List<string> fieldAccessorNames = new List<string>();
PropertyBuilder pb = _tg.DefineProperty(fi.Name, PropertyAttributes.None, fi.FieldType, Type.EmptyTypes);
MethodAttributes methodAttrs = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName;
if (fi.IsStatic) {
methodAttrs |= MethodAttributes.Static;
}
MethodBuilder method;
method = _tg.DefineMethod(FieldGetterPrefix + fi.Name, methodAttrs,
fi.FieldType, Type.EmptyTypes);
ILGen il = new ILGen(method.GetILGenerator());
if (!fi.IsStatic) {
il.EmitLoadArg(0);
}
if (fi.IsLiteral) {
// literal fields need to be inlined directly in here... We use GetRawConstant
// which will work even in partial trust if the constant is protected.
object value = fi.GetRawConstantValue();
switch (Type.GetTypeCode(fi.FieldType)) {
case TypeCode.Boolean:
if ((bool)value) {
il.Emit(OpCodes.Ldc_I4_1);
} else {
il.Emit(OpCodes.Ldc_I4_0);
}
break;
case TypeCode.Byte: il.Emit(OpCodes.Ldc_I4, (byte)value); break;
case TypeCode.Char: il.Emit(OpCodes.Ldc_I4, (char)value); break;
case TypeCode.Double: il.Emit(OpCodes.Ldc_R8, (double)value); break;
case TypeCode.Int16: il.Emit(OpCodes.Ldc_I4, (short)value); break;
case TypeCode.Int32: il.Emit(OpCodes.Ldc_I4, (int)value); break;
case TypeCode.Int64: il.Emit(OpCodes.Ldc_I8, (long)value); break;
case TypeCode.SByte: il.Emit(OpCodes.Ldc_I4, (sbyte)value); break;
case TypeCode.Single: il.Emit(OpCodes.Ldc_R4, (float)value); break;
case TypeCode.String: il.Emit(OpCodes.Ldstr, (string)value); break;
case TypeCode.UInt16: il.Emit(OpCodes.Ldc_I4, (ushort)value); break;
case TypeCode.UInt32: il.Emit(OpCodes.Ldc_I4, (uint)value); break;
case TypeCode.UInt64: il.Emit(OpCodes.Ldc_I8, (ulong)value); break;
}
} else {
il.EmitFieldGet(fi);
}
il.Emit(OpCodes.Ret);
pb.SetGetMethod(method);
fieldAccessorNames.Add(method.Name);
if (!fi.IsLiteral && !fi.IsInitOnly) {
method = _tg.DefineMethod(FieldSetterPrefix + fi.Name, methodAttrs,
null, new Type[] { fi.FieldType });
method.DefineParameter(1, ParameterAttributes.None, "value");
il = new ILGen(method.GetILGenerator());
il.EmitLoadArg(0);
if (!fi.IsStatic) {
il.EmitLoadArg(1);
}
il.EmitFieldSet(fi);
il.Emit(OpCodes.Ret);
pb.SetSetMethod(method);
fieldAccessorNames.Add(method.Name);
}
specialNames[fi.Name] = fieldAccessorNames.ToArray();
}
}
示例6: OverrideConstructor
private void OverrideConstructor(ConstructorInfo parentConstructor) {
ParameterInfo[] pis = parentConstructor.GetParameters();
if (pis.Length == 0 && typeof(IPythonObject).IsAssignableFrom(_baseType)) {
// default ctor on a base type, don't override this one, it assumes
// the PythonType is some default value and we'll always be unique.
return;
}
ParameterInfo[] overrideParams = GetOverrideCtorSignature(pis);
Type[] argTypes = new Type[overrideParams.Length];
string[] paramNames = new string[overrideParams.Length];
for (int i = 0; i < overrideParams.Length; i++) {
argTypes[i] = overrideParams[i].ParameterType;
paramNames[i] = overrideParams[i].Name;
}
ConstructorBuilder cb = _tg.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, argTypes);
for (int i = 0; i < overrideParams.Length; i++) {
ParameterBuilder pb = cb.DefineParameter(i + 1,
overrideParams[i].Attributes,
overrideParams[i].Name);
int origIndex = GetOriginalIndex(pis, overrideParams, i);
if (origIndex >= 0) {
if (pis[origIndex].IsDefined(typeof(ParamArrayAttribute), false)) {
pb.SetCustomAttribute(new CustomAttributeBuilder(
typeof(ParamArrayAttribute).GetConstructor(Type.EmptyTypes), ArrayUtils.EmptyObjects));
} else if (pis[origIndex].IsDefined(typeof(ParamDictionaryAttribute), false)) {
pb.SetCustomAttribute(new CustomAttributeBuilder(
typeof(ParamDictionaryAttribute).GetConstructor(Type.EmptyTypes), ArrayUtils.EmptyObjects));
}
if ((pis[origIndex].Attributes & ParameterAttributes.HasDefault) != 0) {
pb.SetConstant(pis[origIndex].DefaultValue);
}
}
}
ILGen il = new ILGen(cb.GetILGenerator());
int typeArg;
if (pis.Length == 0 || pis[0].ParameterType != typeof(CodeContext)) {
typeArg = 1;
} else {
typeArg = 2;
}
// this.__class__ = <arg?>
// can occur 2 ways:
// 1. If we have our own _typeField then we set it
// 2. If we're a subclass of IPythonObject (e.g. one of our exception classes) then we'll flow it to the
// base type constructor which will set it.
if (!typeof(IPythonObject).IsAssignableFrom(_baseType)) {
il.EmitLoadArg(0);
// base class could have CodeContext parameter in which case our type is the 2nd parameter.
il.EmitLoadArg(typeArg);
il.EmitFieldSet(_typeField);
}
if (_explicitMO != null) {
il.Emit(OpCodes.Ldarg_0);
il.EmitNew(_explicitMO.FieldType.GetConstructor(Type.EmptyTypes));
il.Emit(OpCodes.Stfld, _explicitMO);
}
// initialize all slots to Uninitialized.instance
MethodInfo init = typeof(PythonOps).GetMethod("InitializeUserTypeSlots");
il.EmitLoadArg(0);
il.EmitLoadArg(typeArg);
il.EmitCall(init);
Debug.Assert(_slotsField != null);
il.EmitFieldSet(_slotsField);
CallBaseConstructor(parentConstructor, pis, overrideParams, il);
}
示例7: EmitSetDict
private void EmitSetDict(ILGen gen) {
gen.EmitFieldSet(_dictField);
}
示例8: GenerateConstructor
private ConstructorBuilder GenerateConstructor(TypeBuilder fnTB, Type baseType)
{
ConstructorBuilder cb = fnTB.DefineConstructor(MethodAttributes.Public, CallingConventions.HasThis, CtorTypes());
ILGen gen = new ILGen(cb.GetILGenerator());
//Call base constructor
ConstructorInfo baseCtorInfo = baseType.GetConstructor(Type.EmptyTypes);
gen.EmitLoadArg(0); // gen.Emit(OpCodes.Ldarg_0);
gen.Emit(OpCodes.Call, baseCtorInfo);
//// Initialize __varrev__
//if (_vars.count() > 0)
//{
// gen.EmitLoadArg(0);
// gen.EmitPropertyGet(Compiler.Method_Var_Rev);
// gen.EmitInt(-1);
// gen.Emit(OpCodes.Add);
// gen.EmitFieldSet(_varRevField);
//}
// Store Meta
if (SupportsMeta)
{
gen.EmitLoadArg(0);
gen.EmitLoadArg(1);
gen.Emit(OpCodes.Castclass, typeof(IPersistentMap));
gen.EmitFieldSet(_metaField);
}
// store closed-overs in their fields
int a = 0;
int offset = !SupportsMeta ? 1 : 2;
for (ISeq s = RT.keys(_closes); s != null; s = s.next(), a++)
{
LocalBinding lb = (LocalBinding)s.first();
FieldBuilder fb = _closedOverFields[a];
gen.EmitLoadArg(0); // gen.Emit(OpCodes.Ldarg_0);
gen.EmitLoadArg(a + offset); // gen.Emit(OpCodes.Ldarg, a + 1);
gen.Emit(OpCodes.Stfld, fb);
}
gen.Emit(OpCodes.Ret);
return cb;
}
示例9: GenerateConstructor
private ConstructorBuilder GenerateConstructor(TypeBuilder fnTB, Type baseType)
{
ConstructorBuilder cb = fnTB.DefineConstructor(MethodAttributes.Public, CallingConventions.HasThis, CtorTypes());
ILGen gen = new ILGen(cb.GetILGenerator());
//Call base constructor
ConstructorInfo baseCtorInfo = baseType.GetConstructor(Type.EmptyTypes);
gen.EmitLoadArg(0); // gen.Emit(OpCodes.Ldarg_0);
gen.Emit(OpCodes.Call, baseCtorInfo);
// Store Meta
if (!IsDefType)
{
gen.EmitLoadArg(0);
gen.EmitLoadArg(1);
gen.Emit(OpCodes.Castclass, typeof(IPersistentMap));
gen.EmitFieldSet(_metaField);
}
// store closed-overs in their fields
int a = 0;
int offset = IsDefType ? 1 : 2;
for (ISeq s = RT.keys(_closes); s != null; s = s.next(), a++)
{
LocalBinding lb = (LocalBinding)s.first();
FieldBuilder fb = _closedOverFields[a];
gen.EmitLoadArg(0); // gen.Emit(OpCodes.Ldarg_0);
gen.EmitLoadArg(a + offset); // gen.Emit(OpCodes.Ldarg, a + 1);
gen.Emit(OpCodes.Stfld, fb);
}
gen.Emit(OpCodes.Ret);
return cb;
}
示例10: AddIProxyMethods
private static FieldBuilder AddIProxyMethods(TypeBuilder proxyTB)
{
FieldBuilder fb = proxyTB.DefineField(
_methodMapFieldName,
typeof(IPersistentMap),
FieldAttributes.Private);
MethodBuilder initMb = proxyTB.DefineMethod(
"__initClojureFnMappings",
MethodAttributes.Public|MethodAttributes.Virtual|MethodAttributes.HideBySig,
typeof(void),
new Type[] { typeof(IPersistentMap) });
ILGen gen = new ILGen(initMb.GetILGenerator());
gen.EmitLoadArg(0); // gen.Emit(OpCodes.Ldarg_0);
gen.EmitLoadArg(1); // gen.Emit(OpCodes.Ldarg_1);
gen.EmitFieldSet(fb); // gen.Emit(OpCodes.Stfld, fb);
gen.Emit(OpCodes.Ret);
MethodBuilder updateTB = proxyTB.DefineMethod(
"__updateClojureFnMappings",
MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.HideBySig,
typeof(void),
new Type[] { typeof(IPersistentMap) });
gen = new ILGen(updateTB.GetILGenerator());
gen.EmitLoadArg(0); // gen.Emit(OpCodes.Ldarg_0);
gen.Emit(OpCodes.Dup);
gen.EmitFieldGet(fb); // gen.Emit(OpCodes.Ldfld, fb);
gen.Emit(OpCodes.Castclass, typeof(IPersistentCollection));
gen.EmitLoadArg(1); // gen.Emit(OpCodes.Ldarg_1);
gen.EmitCall(Method_IPersistentCollection_Cons); //gen.Emit(OpCodes.Call, Method_IPersistentCollection_Cons);
gen.EmitFieldSet(fb); // gen.Emit(OpCodes.Stfld, fb);
gen.Emit(OpCodes.Ret);
MethodBuilder getMb = proxyTB.DefineMethod(
"__getClojureFnMappings",
MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.HideBySig,
typeof(IPersistentMap),
Type.EmptyTypes);
gen = new ILGen(getMb.GetILGenerator());
gen.EmitLoadArg(0); // gen.Emit(OpCodes.Ldarg_0);
gen.EmitFieldGet(fb); // gen.Emit(OpCodes.Ldfld, fb);
gen.Emit(OpCodes.Ret);
return fb;
}
示例11: BuildExceptionConstructor
private void BuildExceptionConstructor(ConstructorInfo baseCtor) {
// ctor(RubyClass! class) : base(RubyOps.GetDefaultExceptionMessage(class)) {
// this._class = class;
// }
ConstructorBuilder ctor = _tb.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, _classArgSignature);
ILGen il = new ILGen(ctor.GetILGenerator());
il.EmitLoadArg(0);
il.EmitLoadArg(1);
il.Emit(OpCodes.Call, Methods.GetDefaultExceptionMessage);
il.Emit(OpCodes.Call, baseCtor);
il.EmitLoadArg(0);
il.EmitLoadArg(1);
il.EmitFieldSet(_classField);
il.Emit(OpCodes.Ret);
}
示例12: DefineConstructors
private void DefineConstructors() {
BindingFlags bindingFlags = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance;
ConstructorInfo defaultCtor = _tb.BaseType.GetConstructor(bindingFlags, null, Type.EmptyTypes, null);
// constructor with a single parameter of type RubyClass:
ConstructorInfo defaultRubyCtor = null;
bool deserializerFound = false;
foreach (var baseCtor in _tb.BaseType.GetConstructors(bindingFlags)) {
if (!baseCtor.IsPublic && !baseCtor.IsFamily) {
continue;
}
Type[] paramTypes;
ParameterInfo[] baseParams = baseCtor.GetParameters();
int additionalParamCount;
bool isDeserializer = false;
bool isDefaultRubyCtor = false;
if (baseParams.Length > 0 && baseParams[0].ParameterType == typeof(RubyClass)) {
// Build a simple pass-through constructor
paramTypes = ReflectionUtils.GetParameterTypes(baseParams);
additionalParamCount = 0;
isDefaultRubyCtor = true;
#if !SILVERLIGHT
} else if (baseParams.Length == 2 &&
baseParams[0].ParameterType == typeof(SerializationInfo) && baseParams[1].ParameterType == typeof(StreamingContext)) {
// Build a deserializer
deserializerFound = true;
isDeserializer = true;
paramTypes = ReflectionUtils.GetParameterTypes(baseParams);
additionalParamCount = 0;
#endif
} else {
// Special-case for Exception
if (_tb.IsSubclassOf(typeof(Exception)) && IsAvailable(defaultCtor)) {
if (baseParams.Length == 0) {
// Skip this constructor; it would conflict with the one we're going to build next
continue;
} else if (baseParams.Length == 1 && baseParams[0].ParameterType == typeof(string)) {
// Special case exceptions to improve interop. Ruby's default message for an exception is the name of the exception class.
BuildExceptionConstructor(baseCtor);
}
}
// Add RubyClass to the head of the parameter list
paramTypes = new Type[baseParams.Length + 1];
paramTypes[0] = typeof(RubyClass);
for (int i = 0; i < baseParams.Length; i++) {
paramTypes[i + 1] = baseParams[i].ParameterType;
}
additionalParamCount = 1;
if (baseParams.Length == 0) {
isDefaultRubyCtor = true;
}
}
// Build a new constructor based on this base class ctor
ConstructorBuilder cb = _tb.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, paramTypes);
ILGen il = new ILGen(cb.GetILGenerator());
il.EmitLoadArg(0);
for (int i = 1; i < baseParams.Length + 1; i++) {
il.EmitLoadArg(i + additionalParamCount);
}
il.Emit(OpCodes.Call, baseCtor);
if (!isDeserializer) {
// ctor(RubyClass! class, {params}) : base({params}) { this._class = class; }
il.EmitLoadArg(0);
il.EmitLoadArg(1);
il.EmitFieldSet(_classField);
if (isDefaultRubyCtor) {
defaultRubyCtor = cb;
}
} else {
// ctor(SerializationInfo! info, StreamingContext! context) : base(info, context) {
// RubyOps.DeserializeObject(out this._instanceData, out this._class, info);
// }
il.EmitLoadArg(0);
il.EmitFieldAddress(_instanceDataField);
il.EmitLoadArg(0);
il.EmitFieldAddress(_classField);
il.EmitLoadArg(1);
il.EmitCall(typeof(RubyOps).GetMethod("DeserializeObject"));
}
il.Emit(OpCodes.Ret);
}
#if !SILVERLIGHT
if (defaultRubyCtor != null && !deserializerFound) {
// We didn't previously find a deserialization constructor. If we can, build one now.
BuildDeserializationConstructor(defaultRubyCtor);
}
#endif
}
示例13: EmitSetDict
protected void EmitSetDict(ILGen gen) {
if (_dictField != null) {
gen.EmitFieldSet(_dictField);
} else {
gen.EmitCall(typeof(IPythonObject).GetMethod("ReplaceDict"));
gen.Emit(OpCodes.Pop); // pop bool result
}
}
示例14: DefineConstructors
private void DefineConstructors() {
BindingFlags bindingFlags = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance;
ConstructorInfo emptyCtor = _tb.BaseType.GetConstructor(bindingFlags, null, Type.EmptyTypes, null);
ConstructorInfo classArgCtor = null;
bool deserializerFound = false;
foreach (var baseCtor in _tb.BaseType.GetConstructors(bindingFlags)) {
if (!baseCtor.IsPublic && !baseCtor.IsFamily) {
continue;
}
int oldLength = baseCtor.GetParameters().Length;
List<Type> newParams = new List<Type>(oldLength + 1);
foreach (var param in baseCtor.GetParameters()) {
newParams.Add(param.ParameterType);
}
int offset = 1;
bool isDeserializer = false;
if (oldLength > 0 && newParams[0] == typeof(RubyClass)) {
// Build a simple pass-through constructor
offset = 0;
#if !SILVERLIGHT
} else if (oldLength == 2 && newParams[0] == typeof(SerializationInfo) && newParams[1] == typeof(StreamingContext)) {
// Build a deserializer
deserializerFound = true;
isDeserializer = true;
offset = 0;
#endif
} else {
// Special-case for Exception
if (_tb.IsSubclassOf(typeof(Exception)) && IsAvailable(emptyCtor)) {
if (oldLength == 0) {
// Skip this constructor; it would conflict with the one we're going to build next
continue;
} else if (oldLength == 1 && newParams[0] == typeof(string)) {
// Special case exceptions to improve interop. Ruby's default message for an exception is the name of the exception class.
BuildExceptionConstructor(baseCtor);
}
}
// Add RubyClass to the head of the parameter list
newParams.Insert(0, typeof(RubyClass));
}
// Build a new constructor based on this base class ctor
ConstructorBuilder cb = _tb.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, newParams.ToArray());
ILGen il = new ILGen(cb.GetILGenerator());
il.EmitLoadArg(0);
for (int i = 1; i < newParams.Count; i++) {
il.EmitLoadArg(i + offset);
}
il.Emit(OpCodes.Call, baseCtor);
if (!isDeserializer) {
// ctor(RubyClass! class, ...) : ... { this._class = class; }
il.EmitLoadArg(0);
il.EmitLoadArg(1);
il.EmitFieldSet(_classField);
} else {
// ctor(SerializationInfo! info, StreamingContext! context) : base {
// RubyOps.DeserializeObject(out this._instanceData, out this._class, info);
// }
il.EmitLoadArg(0);
il.EmitFieldAddress(_instanceDataField);
il.EmitLoadArg(0);
il.EmitFieldAddress(_classField);
il.EmitLoadArg(1);
il.EmitCall(typeof(RubyOps).GetMethod("DeserializeObject"));
}
il.Emit(OpCodes.Ret);
if (oldLength == 0) {
classArgCtor = cb;
}
}
#if !SILVERLIGHT
if (classArgCtor != null && !deserializerFound) {
// We didn't previously find a deserialization constructor. If we can, build one now.
BuildDeserializationConstructor(classArgCtor);
}
#endif
}