本文整理汇总了C#中BuildContext.GetItem方法的典型用法代码示例。如果您正苦于以下问题:C# BuildContext.GetItem方法的具体用法?C# BuildContext.GetItem怎么用?C# BuildContext.GetItem使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BuildContext
的用法示例。
在下文中一共展示了BuildContext.GetItem方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: EnsureDelegateType
private static Type EnsureDelegateType(BuildContext context, MethodInfo method)
{
// The delegate should be defined as inner type of context.TypeBuilder.
// It's possible, but we can not define and use newly defined type as Emit target in its owner type.
// To solve this problem, we should create a top level delegate and make sure its name is unique.
//
var delegateName = context.TypeBuilder.TypeBuilder.FullName + "$" + method.Name + "$Delegate";
var delegateType = context.GetItem<Type>(delegateName);
if (delegateType == null)
{
var pi = method.GetParameters();
var parameters = new Type[pi.Length];
for (var i = 0; i < pi.Length; i++)
parameters[i] = pi[i].ParameterType;
const MethodImplAttributes mia = MethodImplAttributes.Runtime | MethodImplAttributes.Managed;
const MethodAttributes ma = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.NewSlot | MethodAttributes.Virtual;
var delegateBuilder = context.AssemblyBuilder.DefineType(delegateName,
TypeAttributes.Class | TypeAttributes.NotPublic | TypeAttributes.Sealed | TypeAttributes.AnsiClass | TypeAttributes.AutoClass,
typeof(MulticastDelegate));
// Create constructor
//
var ctorBuilder = delegateBuilder.DefineConstructor(
MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.RTSpecialName, CallingConventions.Standard,
typeof(object), typeof(IntPtr));
ctorBuilder.ConstructorBuilder.SetImplementationFlags(mia);
// Define the BeginInvoke method for the delegate
//
var beginParameters = new Type[parameters.Length + 2];
Array.Copy(parameters, 0, beginParameters, 0, parameters.Length);
beginParameters[parameters.Length] = typeof(AsyncCallback);
beginParameters[parameters.Length+1] = typeof(object);
var methodBuilder = delegateBuilder.DefineMethod("BeginInvoke", ma, typeof(IAsyncResult), beginParameters);
methodBuilder.MethodBuilder.SetImplementationFlags(mia);
// Define the EndInvoke method for the delegate
//
methodBuilder = delegateBuilder.DefineMethod("EndInvoke", ma, method.ReturnType, typeof(IAsyncResult));
methodBuilder.MethodBuilder.SetImplementationFlags(mia);
// Define the Invoke method for the delegate
//
methodBuilder = delegateBuilder.DefineMethod("Invoke", ma, method.ReturnType, parameters);
methodBuilder.MethodBuilder.SetImplementationFlags(mia);
context.Items.Add(delegateName, delegateType = delegateBuilder.Create());
}
return delegateType;
}