本文整理汇总了C#中ILGen.EmitString方法的典型用法代码示例。如果您正苦于以下问题:C# ILGen.EmitString方法的具体用法?C# ILGen.EmitString怎么用?C# ILGen.EmitString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ILGen
的用法示例。
在下文中一共展示了ILGen.EmitString方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DefineStaticCtor
/// <summary>
/// Set up Var fields and (maybe) load assembly for the namespace.
/// </summary>
/// <param name="proxyTB"></param>
/// <param name="varMap"></param>
/// <param name="loadImplNameSpace"></param>
/// <param name="implNamespace"></param>
private static void DefineStaticCtor(TypeBuilder proxyTB, string prefix, Dictionary<string, FieldBuilder> varMap, bool loadImplNameSpace, string implNamespace, string implCname)
{
ConstructorBuilder cb = proxyTB.DefineConstructor(MethodAttributes.Static, CallingConventions.Standard,Type.EmptyTypes);
ILGen gen = new ILGen(cb.GetILGenerator());
foreach (KeyValuePair<string, FieldBuilder> pair in varMap)
{
gen.EmitString(implNamespace); // gen.Emit(OpCodes.Ldstr, implNamespace);
gen.EmitString(prefix + pair.Key); // gen.Emit(OpCodes.Ldstr, prefix + pair.Key);
gen.EmitCall(Method_Var_internPrivate); // gen.Emit(OpCodes.Call, Method_Var_internPrivate);
gen.Emit(OpCodes.Stsfld, pair.Value);
}
if (loadImplNameSpace)
{
gen.EmitString("clojure.core"); // gen.Emit(OpCodes.Ldstr, "clojure.core");
gen.EmitString("load"); // gen.Emit(OpCodes.Ldstr, "load");
gen.EmitCall(Method_RT_var2); // gen.Emit(OpCodes.Call, Method_RT_var2);
gen.EmitString("/" + implCname); // gen.Emit(OpCodes.Ldstr, "/" + implCname);
gen.EmitCall(Compiler.Methods_IFn_invoke[1]); // gen.Emit(OpCodes.Call, Compiler.Methods_IFn_invoke[1]);
gen.Emit(OpCodes.Pop);
}
gen.Emit(OpCodes.Ret);
}
示例2: EmitUnsupported
private static void EmitUnsupported(ILGen gen, string name)
{
gen.EmitString(name); // gen.Emit(OpCodes.Ldstr, name);
gen.EmitNew(CtorInfo_NotImplementedException_1); // gen.Emit(OpCodes.Newobj, CtorInfo_NotImplementedException_1);
gen.Emit(OpCodes.Throw);
}
示例3: EmitBaseMethodDispatch
/// <summary>
/// Loads all the incoming arguments and forwards them to mi which
/// has the same signature and then returns the result
/// </summary>
private static void EmitBaseMethodDispatch(MethodInfo mi, ILGen il) {
if (!mi.IsAbstract) {
int offset = 0;
if (!mi.IsStatic) {
il.EmitLoadArg(0);
offset = 1;
}
ParameterInfo[] parameters = mi.GetParameters();
for (int i = 0; i < parameters.Length; i++) {
il.EmitLoadArg(i + offset);
}
il.EmitCall(OpCodes.Call, mi, null); // base call must be non-virtual
il.Emit(OpCodes.Ret);
} else {
il.EmitLoadArg(0);
il.EmitString(mi.Name);
il.EmitCall(typeof(PythonOps), "MissingInvokeMethodException");
il.Emit(OpCodes.Throw);
}
}
示例4: SaveToAssembly
/// <summary>
/// This takes an assembly name including extension and saves the provided ScriptCode objects into the assembly.
///
/// The provided script codes can constitute code from multiple languages. The assemblyName can be either a fully qualified
/// or a relative path. The DLR will simply save the assembly to the desired location. The assembly is created by the DLR and
/// if a file already exists than an exception is raised.
///
/// The DLR determines the internal format of the ScriptCode and the DLR can feel free to rev this as appropriate.
/// </summary>
public static void SaveToAssembly(string assemblyName, params SavableScriptCode[] codes) {
ContractUtils.RequiresNotNull(assemblyName, "assemblyName");
ContractUtils.RequiresNotNullItems(codes, "codes");
// break the assemblyName into it's dir/name/extension
string dir = Path.GetDirectoryName(assemblyName);
if (String.IsNullOrEmpty(dir)) {
dir = Environment.CurrentDirectory;
}
string name = Path.GetFileNameWithoutExtension(assemblyName);
string ext = Path.GetExtension(assemblyName);
// build the assembly & type gen that all the script codes will live in...
AssemblyGen ag = new AssemblyGen(new AssemblyName(name), dir, ext, /*emitSymbols*/false);
TypeBuilder tb = ag.DefinePublicType("DLRCachedCode", typeof(object), true);
TypeGen tg = new TypeGen(ag, tb);
var symbolDict = new Dictionary<SymbolId, FieldBuilder>();
// then compile all of the code
Dictionary<Type, List<CodeInfo>> langCtxBuilders = new Dictionary<Type, List<CodeInfo>>();
foreach (SavableScriptCode sc in codes) {
List<CodeInfo> builders;
if (!langCtxBuilders.TryGetValue(sc.LanguageContext.GetType(), out builders)) {
langCtxBuilders[sc.LanguageContext.GetType()] = builders = new List<CodeInfo>();
}
KeyValuePair<MethodBuilder, Type> compInfo = sc.CompileForSave(tg, symbolDict);
builders.Add(new CodeInfo(compInfo.Key, sc, compInfo.Value));
}
MethodBuilder mb = tb.DefineMethod(
"GetScriptCodeInfo",
MethodAttributes.SpecialName | MethodAttributes.Public | MethodAttributes.Static,
typeof(MutableTuple<Type[], Delegate[][], string[][], string[][]>),
Type.EmptyTypes);
ILGen ilgen = new ILGen(mb.GetILGenerator());
var langsWithBuilders = langCtxBuilders.ToArray();
// lang ctx array
ilgen.EmitArray(typeof(Type), langsWithBuilders.Length, (index) => {
ilgen.Emit(OpCodes.Ldtoken, langsWithBuilders[index].Key);
ilgen.EmitCall(typeof(Type).GetMethod("GetTypeFromHandle", new[] { typeof(RuntimeTypeHandle) }));
});
// builders array of array
ilgen.EmitArray(typeof(Delegate[]), langsWithBuilders.Length, (index) => {
List<CodeInfo> builders = langsWithBuilders[index].Value;
ilgen.EmitArray(typeof(Delegate), builders.Count, (innerIndex) => {
ilgen.EmitNull();
ilgen.Emit(OpCodes.Ldftn, builders[innerIndex].Builder);
ilgen.EmitNew(
builders[innerIndex].DelegateType,
new[] { typeof(object), typeof(IntPtr) }
);
});
});
// paths array of array
ilgen.EmitArray(typeof(string[]), langsWithBuilders.Length, (index) => {
List<CodeInfo> builders = langsWithBuilders[index].Value;
ilgen.EmitArray(typeof(string), builders.Count, (innerIndex) => {
ilgen.EmitString(builders[innerIndex].Code.SourceUnit.Path);
});
});
// 4th element in tuple - custom per-language data
ilgen.EmitArray(typeof(string[]), langsWithBuilders.Length, (index) => {
List<CodeInfo> builders = langsWithBuilders[index].Value;
ilgen.EmitArray(typeof(string), builders.Count, (innerIndex) => {
ICustomScriptCodeData data = builders[innerIndex].Code as ICustomScriptCodeData;
if (data != null) {
ilgen.EmitString(data.GetCustomScriptCodeData());
} else {
ilgen.Emit(OpCodes.Ldnull);
}
});
});
ilgen.EmitNew(
typeof(MutableTuple<Type[], Delegate[][], string[][], string[][]>),
new[] { typeof(Type[]), typeof(Delegate[][]), typeof(string[][]), typeof(string[][]) }
);
ilgen.Emit(OpCodes.Ret);
//.........这里部分代码省略.........
示例5: EmitNonInheritedMethodLookup
/// <summary>
/// Emits the call to lookup a member defined in the user's type. Returns
/// the local which stores the resulting value and leaves a value on the
/// stack indicating the success of the lookup.
/// </summary>
private LocalBuilder EmitNonInheritedMethodLookup(string name, ILGen il) {
LocalBuilder callTarget = il.DeclareLocal(typeof(object));
// emit call to helper to do lookup
il.EmitLoadArg(0);
if (typeof(IPythonObject).IsAssignableFrom(_baseType)) {
Debug.Assert(_typeField == null);
il.EmitPropertyGet(TypeInfo._IPythonObject.PythonType);
} else {
il.EmitFieldGet(_typeField);
}
il.EmitLoadArg(0);
il.EmitString(name);
il.Emit(OpCodes.Ldloca, callTarget);
il.EmitCall(typeof(UserTypeOps), "TryGetNonInheritedMethodHelper");
return callTarget;
}
示例6: EmitBaseClassCallCheckForEvents
/// <summary>
/// Emits code to check if the class has overridden this specific
/// function. For example:
///
/// MyDerivedType.SomeVirtualFunction = ...
/// or
///
/// class MyDerivedType(MyBaseType):
/// def SomeVirtualFunction(self, ...):
///
/// </summary>
private static LocalBuilder EmitBaseClassCallCheckForEvents(ILGen il, MethodInfo baseMethod, string name) {
Label instanceCall = il.DefineLabel();
LocalBuilder callTarget = il.DeclareLocal(typeof(object));
il.EmitLoadArg(0);
il.EmitString(name);
il.Emit(OpCodes.Ldloca, callTarget);
il.EmitCall(typeof(UserTypeOps), "TryGetNonInheritedValueHelper");
il.Emit(OpCodes.Brtrue, instanceCall);
EmitBaseMethodDispatch(baseMethod, il);
il.MarkLabel(instanceCall);
return callTarget;
}
示例7: EmitBaseClassCallCheckForProperties
/// <summary>
/// Emits code to check if the class has overridden this specific
/// function. For example:
///
/// MyDerivedType.SomeVirtualFunction = ...
/// or
///
/// class MyDerivedType(MyBaseType):
/// def SomeVirtualFunction(self, ...):
///
/// </summary>
private LocalBuilder EmitBaseClassCallCheckForProperties(ILGen il, MethodInfo baseMethod, string name) {
Label instanceCall = il.DefineLabel();
LocalBuilder callTarget = il.DeclareLocal(typeof(object));
// first lookup under the property name
il.EmitLoadArg(0);
il.EmitString(name);
il.Emit(OpCodes.Ldloca, callTarget);
il.EmitCall(typeof(UserTypeOps), "TryGetNonInheritedValueHelper");
il.Emit(OpCodes.Brtrue, instanceCall);
// then look up under the method name (get_Foo/set_Foo)
LocalBuilder methodTarget = EmitNonInheritedMethodLookup(baseMethod.Name, il);
Label instanceCallMethod = il.DefineLabel();
il.Emit(OpCodes.Brtrue, instanceCallMethod);
// method isn't overridden using either form
EmitBaseMethodDispatch(baseMethod, il);
// we're calling the get_/set_ method
il.MarkLabel(instanceCallMethod);
EmitClrCallStub(il, baseMethod, methodTarget);
il.Emit(OpCodes.Ret);
il.MarkLabel(instanceCall);
// we're accessing a property
return callTarget;
}
示例8: GenerateProxyMethod
private static void GenerateProxyMethod(
TypeBuilder proxyTB,
FieldBuilder mapField,
MethodInfo m,
HashSet<MethodBuilder> specialMethods)
{
MethodAttributes attribs = m.Attributes;
bool callBaseMethod;
if ( (attribs & MethodAttributes.Abstract) == MethodAttributes.Abstract )
{
attribs &= ~MethodAttributes.Abstract;
callBaseMethod = false;
}
else
{
callBaseMethod = true;
}
attribs &= ~MethodAttributes.NewSlot;
attribs |= MethodAttributes.Virtual | MethodAttributes.HideBySig | MethodAttributes.Public;
//Console.Write("Generating proxy method {0}(", m.Name);
//foreach (ParameterInfo p in m.GetParameters())
// Console.Write("{0}, ", p.ParameterType.FullName);
//Console.Write(") ");
//Console.WriteLine(attribs.ToString());
MethodBuilder proxym = proxyTB.DefineMethod(
m.Name,
attribs,
m.CallingConvention,
m.ReturnType,
m.GetParameters().Select<ParameterInfo, Type>(p => p.ParameterType).ToArray<Type>());
if (m.IsSpecialName)
specialMethods.Add(proxym);
ILGen gen = new ILGen(proxym.GetILGenerator());
Label elseLabel = gen.DefineLabel();
Label endLabel = gen.DefineLabel();
//// Print a little message, for debugging purposes
//gen.Emit(OpCodes.Ldstr, String.Format("Calling {0} / {1}", proxyTB.FullName, m.ToString()));
//gen.Emit(OpCodes.Call, typeof(Console).GetMethod("WriteLine",
// new Type[] { typeof(string) }));
//gen.Emit(OpCodes.Call, typeof(Console).GetMethod("get_Out"));
//gen.Emit(OpCodes.Call, typeof(System.IO.TextWriter).GetMethod("Flush"));
// Lookup method name in map
gen.EmitLoadArg(0); // gen.Emit(OpCodes.Ldarg_0);
gen.EmitFieldGet(mapField); // gen.Emit(OpCodes.Ldfld, mapField);
gen.EmitString(m.Name); // gen.Emit(OpCodes.Ldstr, m.Name);
gen.EmitCall(Method_RT_get); // gen.Emit(OpCodes.Call, Method_RT_get);
gen.Emit(OpCodes.Dup);
gen.EmitNull(); // gen.Emit(OpCodes.Ldnull);
gen.Emit(OpCodes.Beq_S, elseLabel);
// map entry found
ParameterInfo[] pinfos = m.GetParameters();
gen.Emit(OpCodes.Castclass, typeof(IFn));
gen.EmitLoadArg(0); // gen.Emit(OpCodes.Ldarg_0); // push implicit 'this' arg.
for (int i = 0; i < pinfos.Length; i++)
{
gen.EmitLoadArg(i + 1); // gen.Emit(OpCodes.Ldarg, i + 1);
if (m.GetParameters()[i].ParameterType.IsValueType)
gen.Emit(OpCodes.Box,pinfos[i].ParameterType);
}
int parmCount = pinfos.Length;
gen.EmitCall(GetIFnInvokeMethodInfo(parmCount+1)); // gen.Emit(OpCodes.Call, GetIFnInvokeMethodInfo(parmCount + 1));
if (m.ReturnType == typeof(void))
gen.Emit(OpCodes.Pop);
else
gen.Emit(OpCodes.Unbox_Any, m.ReturnType);
gen.Emit(OpCodes.Br_S,endLabel);
// map entry not found
gen.MarkLabel(elseLabel);
gen.Emit(OpCodes.Pop); // get rid of null leftover from the 'get'
if ( callBaseMethod )
{
gen.EmitLoadArg(0); // gen.Emit(OpCodes.Ldarg_0);
for (int i = 0; i < parmCount; i++)
gen.EmitLoadArg(i + 1); // gen.Emit(OpCodes.Ldarg, i + 1);
gen.Emit(OpCodes.Call, m); // gen.EmitCall(m) improperly emits a callvirt in some cases
}
else
{
gen.EmitString(m.Name); // gen.Emit(OpCodes.Ldstr, m.Name);
gen.EmitNew(CtorInfo_NotImplementedException_1); // gen.Emit(OpCodes.Newobj, CtorInfo_NotImplementedException_1);
gen.Emit(OpCodes.Throw);
}
gen.MarkLabel(endLabel);
//.........这里部分代码省略.........
示例9: GenerateStatics
protected override void GenerateStatics(GenContext context)
{
if (IsDefType)
{
TypeBuilder tb = TypeBuilder;
// getBasis()
MethodBuilder mbg = tb.DefineMethod("getBasis", MethodAttributes.Public | MethodAttributes.Static, typeof(IPersistentVector), Type.EmptyTypes);
LambdaExpression lambda = Expression.Lambda(GenerateValue(_hintedFields));
lambda.CompileToMethod(mbg, context.IsDebuggable);
if (_fields.count() > _hintedFields.count())
{
// create(IPersistentMap)
MethodBuilder mbc = tb.DefineMethod("create", MethodAttributes.Public | MethodAttributes.Static, tb, new Type[] { typeof(IPersistentMap) });
ILGen gen = new ILGen(mbc.GetILGenerator());
LocalBuilder kwLocal = gen.DeclareLocal(typeof(Keyword));
List<LocalBuilder> locals = new List<LocalBuilder>();
for (ISeq s = RT.seq(_hintedFields); s != null; s = s.next())
{
string bName = ((Symbol)s.first()).Name;
Type t = Compiler.TagType(Compiler.TagOf(s.first()));
// local_kw = Keyword.intern(bname)
// local_i = arg_0.valAt(kw,null)
gen.EmitLoadArg(0);
gen.EmitString(bName);
gen.EmitCall(Compiler.Method_Keyword_intern_string);
gen.Emit(OpCodes.Dup);
gen.Emit(OpCodes.Stloc,kwLocal.LocalIndex);
gen.EmitNull();
gen.EmitCall(Compiler.Method_IPersistentMap_valAt2);
LocalBuilder lb = gen.DeclareLocal(t);
locals.Add(lb);
if ( t.IsPrimitive )
gen.EmitUnbox(t);
gen.Emit(OpCodes.Stloc, lb.LocalIndex);
// arg_0 = arg_0.without(local_kw);
gen.EmitLoadArg(0);
gen.Emit(OpCodes.Ldloc,kwLocal.LocalIndex);
gen.EmitCall(Compiler.Method_IPersistentMap_without);
gen.EmitStoreArg(0);
}
foreach (LocalBuilder lb in locals)
gen.Emit(OpCodes.Ldloc, lb.LocalIndex);
gen.EmitNull();
gen.EmitLoadArg(0);
gen.EmitCall(Compiler.Method_RT_seqOrElse);
gen.EmitNew(CtorInfo);
gen.Emit(OpCodes.Ret);
}
}
}