本文整理汇总了C#中Mono.Cecil.GenericInstanceMethod类的典型用法代码示例。如果您正苦于以下问题:C# GenericInstanceMethod类的具体用法?C# GenericInstanceMethod怎么用?C# GenericInstanceMethod使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
GenericInstanceMethod类属于Mono.Cecil命名空间,在下文中一共展示了GenericInstanceMethod类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: MakeGenericMethod
public static GenericInstanceMethod MakeGenericMethod(this MethodReference method, params TypeReference[] genericArguments)
{
var result = new GenericInstanceMethod(method);
foreach (var argument in genericArguments)
result.GenericArguments.Add(argument);
return result;
}
示例2: createCallResult
protected override CallResult createCallResult(MethodReference method, GenericInstanceMethod gim, Block block, int callInstrIndex)
{
int methodId;
if (!methodTokenToId.TryGetValue(method.MetadataToken.ToInt32(), out methodId))
return null;
return new MyCallResult(block, callInstrIndex, methodId, gim);
}
示例3: GetEquality
MethodReference GetEquality(TypeReference typeDefinition)
{
if (typeDefinition.IsArray)
{
return null;
}
if (typeDefinition.IsGenericParameter)
{
return null;
}
if (typeDefinition.Namespace.StartsWith("System.Collections"))
{
return null;
}
if (typeDefinition.IsGenericInstance)
{
if (typeDefinition.FullName.StartsWith("System.Nullable"))
{
var genericInstanceMethod = new GenericInstanceMethod(NullableEqualsMethod);
var typeWrappedByNullable = ((GenericInstanceType) typeDefinition).GenericArguments.First();
genericInstanceMethod.GenericArguments.Add(typeWrappedByNullable);
return typeWrappedByNullable.IsGenericParameter ? ModuleDefinition.Import(genericInstanceMethod, typeWrappedByNullable.DeclaringType) : ModuleDefinition.Import(genericInstanceMethod);
}
}
var equality = GetStaticEquality(typeDefinition);
if (equality != null)
{
return ModuleDefinition.Import(equality);
}
return null;
}
示例4: MakeGenericMethod
public static GenericInstanceMethod MakeGenericMethod(this MethodReference reference, params TypeReference[] arguments)
{
var result = new GenericInstanceMethod(reference);
foreach (var t in arguments)
result.GenericArguments.Add(t);
return result;
}
示例5: MakeGenericMethod
public static MethodReference MakeGenericMethod(this MethodReference self, params TypeReference[] arguments)
{
var method = new GenericInstanceMethod(self);
foreach(var argument in arguments)
method.GenericArguments.Add(argument);
return method;
}
示例6: InjectField
void InjectField(TypeDefinition type, FieldDefinition fieldDefinition)
{
var staticConstructor = type.GetStaticConstructor();
staticConstructor.Body.SimplifyMacros();
var genericInstanceMethod = new GenericInstanceMethod(forContextDefinition);
if (type.IsCompilerGenerated() && type.IsNested)
{
genericInstanceMethod.GenericArguments.Add(type.DeclaringType.GetGeneric());
}
else
{
genericInstanceMethod.GenericArguments.Add(type.GetGeneric());
}
var instructions = staticConstructor.Body.Instructions;
type.Fields.Add(fieldDefinition);
var returns = instructions.Where(_ => _.OpCode == OpCodes.Ret)
.ToList();
var ilProcessor = staticConstructor.Body.GetILProcessor();
foreach (var returnInstruction in returns)
{
var newReturn = Instruction.Create(OpCodes.Ret);
ilProcessor.InsertAfter(returnInstruction, newReturn);
ilProcessor.InsertBefore(newReturn, Instruction.Create(OpCodes.Call, genericInstanceMethod));
ilProcessor.InsertBefore(newReturn, Instruction.Create(OpCodes.Stsfld, fieldDefinition.GetGeneric()));
returnInstruction.OpCode = OpCodes.Nop;
}
staticConstructor.Body.OptimizeMacros();
}
示例7: Instance
//that one is for normal use - like calling class members, etc
public static MethodReference Instance(this MethodDefinition self)
{
var declaringType = self.DeclaringType.Instance();
var reference = new MethodReference(self.Name, self.ReturnType, declaringType);
reference.HasThis = self.HasThis;
reference.ExplicitThis = self.ExplicitThis;
reference.CallingConvention = self.CallingConvention;
foreach (var parameter in self.Parameters)
reference.Parameters.Add(new ParameterDefinition(parameter.ParameterType));
foreach (var generic_parameter in self.GenericParameters)
reference.GenericParameters.Add(new GenericParameter(generic_parameter.Name, reference));
if (self.HasGenericParameters && !self.IsGetter && !self.IsSetter)
{
reference = new GenericInstanceMethod(reference);
foreach (var genericParam in self.GenericParameters)
if (self.DeclaringType.GenericParameters.SingleOrDefault(m => m.Name == genericParam.Name) == null)
((GenericInstanceMethod)(reference)).GenericArguments.Add(genericParam);
}
return reference;
}
示例8: ExecuteWinRT
public void ExecuteWinRT()
{
var assemblyResolver = ModuleDefinition.AssemblyResolver;
var systemRuntime = assemblyResolver.Resolve("System.Runtime");
var systemRuntimeTypes = systemRuntime.MainModule.Types;
var objectDefinition = systemRuntimeTypes.First(x => x.Name == "Object");
var constructorDefinition = objectDefinition.Methods.First(x => x.IsConstructor);
ObjectConstructor = ModuleDefinition.ImportReference(constructorDefinition);
var objectEqualsMethodDefinition = objectDefinition.Methods.First(x => x.Name == "Equals" && x.Parameters.Count == 2);
ObjectEqualsMethod = ModuleDefinition.ImportReference(objectEqualsMethodDefinition);
var nullableDefinition = systemRuntimeTypes.FirstOrDefault(x => x.Name == "Nullable");
NullableEqualsMethod = ModuleDefinition.ImportReference(nullableDefinition).Resolve().Methods.First(x => x.Name == "Equals");
EqualityComparerTypeReference = systemRuntimeTypes.FirstOrDefault(x => x.Name == "EqualityComparer`1");
var actionDefinition = systemRuntimeTypes.First(x => x.Name == "Action");
ActionTypeReference = ModuleDefinition.ImportReference(actionDefinition);
var actionConstructor = actionDefinition.Methods.First(x => x.IsConstructor);
ActionConstructorReference = ModuleDefinition.ImportReference(actionConstructor);
var systemObjectModel = assemblyResolver.Resolve("System.ObjectModel");
var systemObjectModelTypes = systemObjectModel.MainModule.Types;
var propChangedInterfaceDefinition = systemObjectModelTypes.First(x => x.Name == "INotifyPropertyChanged");
PropChangedInterfaceReference = ModuleDefinition.ImportReference(propChangedInterfaceDefinition);
var propChangedHandlerDefinition = systemObjectModelTypes.First(x => x.Name == "PropertyChangedEventHandler");
PropChangedHandlerReference = ModuleDefinition.ImportReference(propChangedHandlerDefinition);
ComponentModelPropertyChangedEventHandlerInvokeReference = ModuleDefinition.ImportReference(propChangedHandlerDefinition.Methods.First(x => x.Name == "Invoke"));
var propChangedArgsDefinition = systemObjectModelTypes.First(x => x.Name == "PropertyChangedEventArgs");
ComponentModelPropertyChangedEventConstructorReference = ModuleDefinition.ImportReference(propChangedArgsDefinition.Methods.First(x => x.IsConstructor));
var delegateDefinition = systemRuntimeTypes.First(x => x.Name == "Delegate");
var combineMethodDefinition = delegateDefinition.Methods
.Single(x =>
x.Name == "Combine" &&
x.Parameters.Count == 2 &&
x.Parameters.All(p => p.ParameterType == delegateDefinition));
DelegateCombineMethodRef = ModuleDefinition.ImportReference(combineMethodDefinition);
var removeMethodDefinition = delegateDefinition.Methods.First(x => x.Name == "Remove");
DelegateRemoveMethodRef = ModuleDefinition.ImportReference(removeMethodDefinition);
var systemThreading = assemblyResolver.Resolve("System.Threading");
var interlockedDefinition = systemThreading.MainModule.Types.First(x => x.FullName == "System.Threading.Interlocked");
var genericCompareExchangeMethodDefinition = interlockedDefinition
.Methods.First(x =>
x.IsStatic &&
x.Name == "CompareExchange" &&
x.GenericParameters.Count == 1 &&
x.Parameters.Count == 3);
var genericCompareExchangeMethod = ModuleDefinition.ImportReference(genericCompareExchangeMethodDefinition);
InterlockedCompareExchangeForPropChangedHandler = new GenericInstanceMethod(genericCompareExchangeMethod);
InterlockedCompareExchangeForPropChangedHandler.GenericArguments.Add(PropChangedHandlerReference);
}
示例9: Assets
public Assets([NotNull] ModuleWeaver moduleWeaver)
{
if (moduleWeaver == null)
{
throw new ArgumentNullException("moduleWeaver");
}
_commands = new ConcurrentDictionary<string, CommandData>();
_moduleDefinition = moduleWeaver.ModuleDefinition;
_log = moduleWeaver;
var settings = moduleWeaver.Settings;
_allClasses = new Lazy<List<TypeDefinition>>(()=> settings.GetAllTypes(moduleWeaver).ToList());
var types = new Types(moduleWeaver);
_typeReferences = types;
_typeDefinitions = types;
var constructorDefinition = TypeDefinitions.Object.Methods.First(x => x.IsConstructor);
_objectConstructorReference = ModuleDefinition.Import(constructorDefinition);
var actionConstructor = TypeDefinitions.Action.Methods.First(x => x.IsConstructor);
ActionConstructorReference = ModuleDefinition.Import(actionConstructor);
var actionOfTConstructor = TypeDefinitions.ActionOfT.GetConstructors().First();
_actionOfTConstructorReference = ModuleDefinition.Import(actionOfTConstructor);
var actionOfTInvokerDefinition = TypeDefinitions.ActionOfT.Methods.First(x => x.Name == "Invoke");
_actionOfTInvokeReference = ModuleDefinition.Import(actionOfTInvokerDefinition);
var funcConstructor = TypeDefinitions.FuncOfT.Resolve().Methods.First(m => m.IsConstructor && m.Parameters.Count == 2);
_funcOfBoolConstructorReference = ModuleDefinition.Import(funcConstructor).MakeHostInstanceGeneric(TypeReferences.Boolean);
var predicateOfTConstructor = TypeDefinitions.PredicateOfT.GetConstructors().First();
_predicateOfTConstructorReference = ModuleDefinition.Import(predicateOfTConstructor);
var predicateOfTInvokerDefinition = TypeDefinitions.PredicateOfT.Methods.First(x => x.Name == "Invoke");
_predicateOfTInvokeReference = ModuleDefinition.Import(predicateOfTInvokerDefinition);
var delegateCombineDefinition = TypeDefinitions.Delegate.Methods.First(x => x.Name == "Combine" && x.Parameters.Count == 2);
_delegateCombineMethodReference = ModuleDefinition.Import(delegateCombineDefinition);
var delegateRemoveDefinition = TypeDefinitions.Delegate.Methods.First(x => x.Name == "Remove" && x.Parameters.Count == 2);
_delegateRemoveMethodReference = ModuleDefinition.Import(delegateRemoveDefinition);
var interlockedCompareExchangeMethodDefinition = TypeDefinitions.Interlocked.Methods.First(
x => x.Name == "CompareExchange"
&& x.Parameters.Count == 3
&& x.HasGenericParameters
);
_interlockedCompareExchangeOfT = ModuleDefinition.Import(interlockedCompareExchangeMethodDefinition);
_interlockedCompareExchangeOfEventHandler = new GenericInstanceMethod(_interlockedCompareExchangeOfT);
_interlockedCompareExchangeOfEventHandler.GenericArguments.Add(TypeReferences.EventHandler);
//_interlockedCompareExchangeOfEventHandler =
if (TypeDefinitions.CommandManager != null)
{
var requeryEvent = TypeDefinitions.CommandManager.Resolve().Events.Single(evt => evt.Name == "RequerySuggested");
_commandManagerAddRequerySuggestedMethodReference = ModuleDefinition.Import(requeryEvent.AddMethod);
_commandManagerRemoveRequerySuggestedMethodReference = ModuleDefinition.Import(requeryEvent.RemoveMethod);
}
_commandImplementationConstructors = GetCommandImplementationConstructors();
constructorDefinition =
TypeDefinitions.ArgumentNullException.Methods.Single(
x => x.IsConstructor && x.HasParameters && x.Parameters.Count == 1);
_argumentNullExceptionConstructorReference = ModuleDefinition.Import(constructorDefinition);
}
示例10: CreateNewObjectMethod
public bool CreateNewObjectMethod(AssemblyDefinition assembly,
MethodDefinition templateMethod,
IAssemblyTracker tracker,
INewTransformerInfoWrapper infoWrapper)
{
MethodDefinition factoryMethod = null;
if ((factoryMethod = infoWrapper.GetFactoryMethod (templateMethod, assembly, tracker)) != null)
{
if (factoryMethod.GenericParameters.Count != 1 || factoryMethod.Parameters.Count != 1 || !factoryMethod.IsStatic)
throw new ArgumentException ("Factory method to create object does not have correct signature [public static T Create<T> (ParamList)]");
TypeReference returnType = templateMethod.DeclaringType;
if (templateMethod.DeclaringType.HasGenericParameters)
{
returnType = new GenericInstanceType (templateMethod.DeclaringType);
foreach (var a in templateMethod.DeclaringType.GenericParameters.ToArray ())
{
returnType.GenericParameters.Add (a);
((GenericInstanceType) returnType).GenericArguments.Add (a);
}
}
var importedFactoryMethod = templateMethod.Module.Import (factoryMethod);
var genericInstanceMethod = new GenericInstanceMethod (importedFactoryMethod);
genericInstanceMethod.GenericArguments.Add (templateMethod.DeclaringType);
var paramlistDef = factoryMethod.Parameters[0].ParameterType.Resolve ();
var importedParamListCreateMethod = templateMethod.Module.Import (SearchParamListFactoryMethod (paramlistDef, templateMethod));
if (importedParamListCreateMethod == null)
throw new ArgumentException ("Factory method: no corresponding 'create' method could have been found. [argument count]");
var newObjectMethod = new MethodDefinition (
infoWrapper.GetWrapperMethodName (templateMethod),
MethodAttributes.Public | MethodAttributes.Static, returnType
);
var instructions = newObjectMethod.Body.Instructions;
foreach (var param in templateMethod.Parameters)
{
newObjectMethod.Parameters.Add (param);
instructions.Add (Instruction.Create (OpCodes.Ldarg, param));
}
instructions.Add(Instruction.Create (OpCodes.Call, importedParamListCreateMethod));
instructions.Add(Instruction.Create (OpCodes.Call, genericInstanceMethod));
instructions.Add (Instruction.Create (OpCodes.Ret));
newObjectMethod.Body.OptimizeMacros ();
newObjectMethod.IsHideBySig = true;
templateMethod.DeclaringType.Methods.Add (newObjectMethod);
return true;
}
return false;
}
示例11: MakeGenericMethod
public static MethodReference MakeGenericMethod (this MethodReference self, params TypeReference [] arguments)
{
if (self.GenericParameters.Count != arguments.Length)
throw new ArgumentException ();
var instance = new GenericInstanceMethod (self);
foreach (var argument in arguments)
instance.GenericArguments.Add (argument);
return instance;
}
示例12: GenericBindingContext
private GenericBindingContext(GenericInstanceMethod method)
{
var definition = method.Resolve();
for (int i = 0; i < definition.GenericParameters.Count; ++i)
{
var parameter = definition.GenericParameters[i];
var binding = method.GenericArguments[i];
_methodEntries.Add(new Entry(parameter.FullName, binding));
}
}
示例13: CallEventInvoker
public Instruction CallEventInvoker(PropertyDefinition propertyDefinition)
{
var method = typeNode.EventInvoker.MethodReference;
if (method.HasGenericParameters)
{
var genericMethod = new GenericInstanceMethod(method);
genericMethod.GenericArguments.Add(propertyDefinition.PropertyType);
method = genericMethod;
}
return Instruction.Create(OpCodes.Callvirt, method);
}
示例14: ProcessGenericType
internal static void ProcessGenericType(GenericInstanceType type, InflatedCollectionCollector generics, GenericInstanceMethod contextMethod, CollectionMode mode)
{
bool flag = generics.TypeDeclarations.Add(type);
if (((mode != CollectionMode.Types) || flag) && ((mode != CollectionMode.MethodsAndTypes) || generics.TypeMethodDeclarations.Add(type)))
{
GenericContext genericContext = new GenericContext(type, contextMethod);
Unity.Cecil.Visitor.Extensions.Accept(type.ElementType.Resolve(), new GenericContextAwareDeclarationOnlyVisitor(generics, genericContext, CollectionMode.Types));
foreach (GenericInstanceType type2 in Enumerable.OfType<GenericInstanceType>(type.GenericArguments))
{
ProcessGenericType(Inflater.InflateType(genericContext, type2), generics, null, mode);
}
}
}
示例15: Convert
public Either<MemberReference, PropertyReferenceContainer> Convert(IAstReference astReference, ModuleDefinition module, IReferenceProvider recursive)
{
var methodAst = astReference as AstGenericMethodWithTypeArguments;
if (methodAst == null)
return null;
var method = recursive.Convert(methodAst.Actual, module, recursive).As<MethodReference>();
var typeArguments = methodAst.GenericArgumentTypes.Select(a => recursive.Convert(a, module, recursive).As<TypeReference>());
var result = new GenericInstanceMethod(method);
typeArguments.ForEach(result.GenericArguments.Add);
return result;
}