本文整理汇总了C#中ILGen.EmitType方法的典型用法代码示例。如果您正苦于以下问题:C# ILGen.EmitType方法的具体用法?C# ILGen.EmitType怎么用?C# ILGen.EmitType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ILGen
的用法示例。
在下文中一共展示了ILGen.EmitType方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: EmitConvertFromObject
/// <summary>
/// Emit code to convert object to a given type. This code is semantically equivalent
/// to PythonBinder.EmitConvertFromObject, except this version accepts ILGen whereas
/// PythonBinder accepts Compiler. The Binder will chagne soon and the two will merge.
/// </summary>
private static void EmitConvertFromObject(ILGen il, Type toType) {
if (toType == typeof(object)) return;
if (toType.IsGenericParameter) {
il.EmitCall(typeof(PythonOps).GetMethod("ConvertFromObject").MakeGenericMethod(toType));
return;
}
MethodInfo fastConvertMethod = PythonBinder.GetFastConvertMethod(toType);
if (fastConvertMethod != null) {
il.EmitCall(fastConvertMethod);
} else if (toType == typeof(void)) {
il.Emit(OpCodes.Pop);
} else if (typeof(Delegate).IsAssignableFrom(toType)) {
il.EmitType(toType);
il.EmitCall(typeof(Converter), "ConvertToDelegate");
il.Emit(OpCodes.Castclass, toType);
} else {
Label end = il.DefineLabel();
il.Emit(OpCodes.Dup);
il.Emit(OpCodes.Isinst, toType);
il.Emit(OpCodes.Brtrue_S, end);
il.Emit(OpCodes.Ldtoken, toType);
il.EmitCall(PythonBinder.GetGenericConvertMethod(toType));
il.MarkLabel(end);
il.Emit(OpCodes.Unbox_Any, toType); //??? this check may be redundant
}
}
示例2: EmitClrCallStub
/// <summary>
/// Generates stub to receive the CLR call and then call the dynamic language code.
/// This code is same as StubGenerator.cs in the Microsoft.Scripting, except it
/// accepts ILGen instead of Compiler.
/// </summary>
private void EmitClrCallStub(ILGen il, MethodInfo mi, LocalBuilder callTarget) {
int firstArg = 0;
bool list = false; // The list calling convention
bool context = false; // Context is an argument
ParameterInfo[] pis = mi.GetParameters();
if (pis.Length > 0) {
if (pis[0].ParameterType == typeof(CodeContext)) {
firstArg = 1;
context = true;
}
if (pis[pis.Length - 1].IsDefined(typeof(ParamArrayAttribute), false)) {
list = true;
}
}
ParameterInfo[] args = pis;
int nargs = args.Length - firstArg;
Type[] genericArgs = mi.GetGenericArguments();
// Create the action
ILGen cctor = GetCCtor();
if (list || genericArgs.Length > 0) {
// Use a complex call signature that includes param array and keywords
cctor.EmitInt(nargs);
cctor.EmitBoolean(list);
// Emit an array of keyword names:
cctor.EmitInt(genericArgs.Length);
cctor.Emit(OpCodes.Newarr, typeof(string));
for (int i = 0; i < genericArgs.Length; i++) {
cctor.Emit(OpCodes.Dup);
cctor.EmitInt(i);
cctor.Emit(OpCodes.Ldelema, typeof(string));
cctor.Emit(OpCodes.Ldstr, genericArgs[i].Name);
cctor.Emit(OpCodes.Stobj, typeof(string));
}
cctor.EmitCall(typeof(PythonOps).GetMethod("MakeComplexCallAction"));
} else {
cctor.EmitInt(nargs);
cctor.EmitCall(typeof(PythonOps).GetMethod("MakeSimpleCallAction"));
}
// Create the dynamic site
Type siteType = CompilerHelpers.MakeCallSiteType(MakeSiteSignature(nargs + genericArgs.Length));
FieldBuilder site = _tg.DefineField("site$" + _site++, siteType, FieldAttributes.Private | FieldAttributes.Static);
cctor.EmitCall(siteType.GetMethod("Create"));
cctor.EmitFieldSet(site);
List<ReturnFixer> fixers = new List<ReturnFixer>(0);
//
// Emit the site invoke
//
il.EmitFieldGet(site);
FieldInfo target = siteType.GetField("Target");
il.EmitFieldGet(target);
il.EmitFieldGet(site);
// Emit the code context
EmitCodeContext(il, context);
il.Emit(OpCodes.Ldloc, callTarget);
for (int i = firstArg; i < args.Length; i++) {
ReturnFixer rf = ReturnFixer.EmitArgument(il, args[i], i + 1);
if (rf != null) {
fixers.Add(rf);
}
}
for (int i = 0; i < genericArgs.Length; i++) {
il.EmitType(genericArgs[i]);
il.EmitCall(typeof(DynamicHelpers).GetMethod("GetPythonTypeFromType"));
}
il.EmitCall(target.FieldType, "Invoke");
foreach (ReturnFixer rf in fixers) {
rf.FixReturn(il);
}
EmitConvertFromObject(il, mi.ReturnType);
}
示例3: EmitConvertFromObject
/// <summary>
/// Emit code to convert object to a given type. This code is semantically equivalent
/// to PythonBinder.EmitConvertFromObject, except this version accepts ILGen whereas
/// PythonBinder accepts Compiler. The Binder will chagne soon and the two will merge.
/// </summary>
public void EmitConvertFromObject(ILGen il, Type toType) {
if (toType == typeof(object)) return;
MethodInfo fastConvertMethod = GetFastConvertMethod(toType);
if (fastConvertMethod != null) {
il.EmitCall(fastConvertMethod);
} else if (toType == typeof(void)) {
il.Emit(OpCodes.Pop);
} else if (typeof(Delegate).IsAssignableFrom(toType)) {
il.EmitType(toType);
il.EmitCall(ConvertToDelegate());
il.Emit(OpCodes.Castclass, toType);
} else {
Label end = il.DefineLabel();
il.Emit(OpCodes.Dup);
il.Emit(OpCodes.Isinst, toType);
il.Emit(OpCodes.Brtrue_S, end);
il.Emit(OpCodes.Ldtoken, toType);
il.EmitCall(GetGenericConvertMethod(toType));
il.MarkLabel(end);
il.Emit(OpCodes.Unbox_Any, toType); //??? this check may be redundant
}
}