本文整理汇总了C#中Mono.Cecil.GenericInstanceType类的典型用法代码示例。如果您正苦于以下问题:C# GenericInstanceType类的具体用法?C# GenericInstanceType怎么用?C# GenericInstanceType使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
GenericInstanceType类属于Mono.Cecil命名空间,在下文中一共展示了GenericInstanceType类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: MakeHostInstanceGeneric
public static MethodReference MakeHostInstanceGeneric(this MethodReference @this, params TypeReference[] genericArguments)
{
var genericDeclaringType = new GenericInstanceType(@this.DeclaringType);
foreach (var genericArgument in genericArguments)
{
genericDeclaringType.GenericArguments.Add(genericArgument);
}
var reference = new MethodReference(@this.Name, @this.ReturnType, genericDeclaringType)
{
HasThis = @this.HasThis,
ExplicitThis = @this.ExplicitThis,
CallingConvention = @this.CallingConvention
};
foreach (var parameter in @this.Parameters)
{
reference.Parameters.Add(new ParameterDefinition(parameter.ParameterType));
}
foreach (var genericParam in @this.GenericParameters)
{
reference.GenericParameters.Add(new GenericParameter(genericParam.Name, reference));
}
return reference;
}
示例2: EmitArchsInit
private static void EmitArchsInit(MethodBody body, FieldReference archRef, Action<Instruction> emit)
{
var module = body.Method.Module;
GenericInstanceType dictStrStrRef = (GenericInstanceType)archRef.FieldType;
TypeReference dictOpenRef = dictStrStrRef.ElementType;
GenericInstanceType iEqCompStrRef = new GenericInstanceType(module.Import(typeof(IEqualityComparer<>)));
iEqCompStrRef.GenericArguments.Add(dictOpenRef.GenericParameters[0]);
MethodReference dictStrStrCtor = CecilUtils.ImportInstanceMethodRef(module, dictStrStrRef, ".ctor", null, iEqCompStrRef);
MethodReference dictAddRef = CecilUtils.ImportInstanceMethodRef(module, dictStrStrRef, "Add", null, dictOpenRef.GenericParameters[0], dictOpenRef.GenericParameters[1]);
// Variables
body.Variables.Add(new VariableDefinition(dictStrStrRef));
int varIdx = body.Variables.Count - 1;
Instruction varSt = CecilUtils.ShortestStloc(varIdx);
Instruction varLd = CecilUtils.ShortestLdloc(varIdx);
emit(Instruction.Create(OpCodes.Ldnull));
emit(Instruction.Create(OpCodes.Newobj, dictStrStrCtor));
emit(varSt.Clone());
emit(varLd.Clone());
emit(Instruction.Create(OpCodes.Stsfld, archRef));
Action<string, string> emitAddPair = (k, v) =>
{
emit(varLd.Clone());
emit(Instruction.Create(OpCodes.Ldstr, k));
emit(Instruction.Create(OpCodes.Ldstr, v));
emit(Instruction.Create(OpCodes.Callvirt, dictAddRef));
};
emitAddPair("x86", "Win32");
emitAddPair("AMD64", "x64");
emitAddPair("IA64", "Itanium");
emitAddPair("ARM", "WinCE");
}
示例3: CheckGenericArgument
static bool CheckGenericArgument (GenericInstanceType git)
{
if ((git == null) || !git.HasGenericArguments)
return false; // should not happen with the '`1' but...
TypeReference arg = git.GenericArguments [0];
switch (arg.MetadataType) {
case MetadataType.MVar:
return (arg.IsGenericParameter && arg.IsNamed (String.Empty, "T"));
case MetadataType.ValueType:
return arg.IsNamed ("System", "Decimal");
case MetadataType.Boolean:
case MetadataType.Byte:
case MetadataType.Char:
case MetadataType.Double:
case MetadataType.Single:
case MetadataType.Int16:
case MetadataType.Int32:
case MetadataType.Int64:
case MetadataType.SByte:
case MetadataType.UInt16:
case MetadataType.UInt32:
case MetadataType.UInt64:
return true;
default:
return false;
}
}
开发者ID:col42dev,项目名称:mono-tools,代码行数:28,代码来源:PreferInterfaceConstraintOnGenericParameterForPrimitiveInterfaceRule.cs
示例4: ResolveGenericParameters
private TypeReference ResolveGenericParameters(TypeReference typeReference)
{
if (typeReference.IsGenericParameter)
{
GenericParameter parameter = (GenericParameter) typeReference;
return Enumerable.ElementAt<TypeReference>(this.GenericArguments, parameter.Position);
}
if (typeReference.IsGenericInstance)
{
GenericInstanceType type = (GenericInstanceType) typeReference;
GenericInstanceType type2 = new GenericInstanceType(this.ResolveGenericParameters(type.ElementType));
foreach (TypeReference reference2 in type.GenericArguments)
{
type2.GenericArguments.Add(this.ResolveGenericParameters(reference2));
}
return type2;
}
if (typeReference.IsArray)
{
ArrayType type3 = (ArrayType) typeReference;
return new ArrayType(this.ResolveGenericParameters(type3.ElementType), type3.Rank);
}
if (typeReference.IsPointer)
{
return new PointerType(this.ResolveGenericParameters(((PointerType) typeReference).ElementType));
}
if (typeReference.IsByReference)
{
return new ByReferenceType(this.ResolveGenericParameters(((ByReferenceType) typeReference).ElementType));
}
return typeReference;
}
示例5: Generate
protected override AssemblyDefinition Generate()
{
var assembly = AssemblyFactory.DefineAssembly(DllName, TargetRuntime.NET_2_0, AssemblyKind.Dll);
var objectTypeRef = assembly.MainModule.Import(typeof (object));
var module = assembly.MainModule;
var xType = new TypeDefinition("X", "", TypeAttributes.Class, objectTypeRef);
xType.GenericParameters.Add(new GenericParameter("T", xType));
module.Types.Add(xType);
xType.Module = module;
var aType = new TypeDefinition("A", "", TypeAttributes.Class, null);
var bType = new TypeDefinition("B", "", TypeAttributes.Class, null);
var aBaseType = new GenericInstanceType(xType);
aBaseType.GenericArguments.Add(bType);
aType.BaseType = aBaseType;
var bBaseType = new GenericInstanceType(xType);
bBaseType.GenericArguments.Add(aType);
bType.BaseType = bBaseType;
module.Types.Add(aType);
aType.Module = module;
module.Types.Add(bType);
bType.Module = module;
return assembly;
}
示例6: ResolveConstituentType
public TypeReference ResolveConstituentType(TypeReference sourceType)
{
if (sourceType == null) throw new ArgumentNullException("sourceType");
sourceType = Import(sourceType);
if (sourceType is GenericParameter) {
return _map[sourceType.Name];
}
{
var sourceTypeAsGenericInstance = sourceType as GenericInstanceType;
if (sourceTypeAsGenericInstance != null) {
var targetType = new GenericInstanceType(sourceTypeAsGenericInstance.ElementType);
foreach (TypeReference sourceArgument in sourceTypeAsGenericInstance.GenericArguments) {
var targetArgument = ResolveConstituentType(sourceArgument);
targetType.GenericArguments.Add(targetArgument);
}
return targetType;
}
}
{
var sourceTypeAsArray = sourceType as ArrayType;
if (sourceTypeAsArray != null) {
return new ArrayType(ResolveConstituentType(sourceTypeAsArray.ElementType));
}
}
{
var sourceTypeAsReference = sourceType as ByReferenceType;
if (sourceTypeAsReference != null) {
return new ByReferenceType(ResolveConstituentType(sourceTypeAsReference.ElementType));
}
}
{
var sourceTypeAsOptional = sourceType as OptionalModifierType;
if (sourceTypeAsOptional != null) {
return new OptionalModifierType(ResolveConstituentType(sourceTypeAsOptional.ElementType), ResolveConstituentType(sourceTypeAsOptional.ModifierType));
}
}
{
var sourceTypeAsRequired = sourceType as RequiredModifierType;
if (sourceTypeAsRequired != null) {
return new RequiredModifierType(ResolveConstituentType(sourceTypeAsRequired.ElementType), ResolveConstituentType(sourceTypeAsRequired.ModifierType));
}
}
// TODO:
//FunctionPointerType?
//SentinelType??
// PinnedType is never used as a parameter (TODO: or is it?)
// PointerType never has a generic element type
return sourceType;
}
示例7: RecursiveGenericDepthFor
public static int RecursiveGenericDepthFor(GenericInstanceType type)
{
if (type == null)
{
return 0;
}
return RecursiveGenericDepthFor(type, !type.HasGenericArguments ? 0 : 1);
}
示例8: AddICollectionTProxy
private static void AddICollectionTProxy(ModuleDefinition moduleDefinition, TypeDefinition type, GenericInstanceType collectionT)
{
var itemType = collectionT.GenericArguments[0];
var itemArray = itemType.MakeArrayType();
var proxyType = CreateProxy(moduleDefinition, type);
TypeReference proxyTypeRef = proxyType;
if (type.HasGenericParameters)
proxyTypeRef = proxyType.MakeGenericInstanceType(type.GenericParameters.Select(gp => new GenericParameter(gp.Position, gp.Type, gp.Module)).ToArray());
var field = proxyType.Fields[0];
var fieldRef = new FieldReference(field.Name, field.FieldType, proxyTypeRef);
var countProperty = type.Properties.First(p => p.Name == "Count" || p.Name == "System.Collections.ICollection.Count");
MethodReference countMethod = countProperty.GetMethod;
MethodReference copyToMethod = type.Methods.First(p => p.Name == "CopyTo" || p.Name == "System.Collections.ICollection.CopyTo");
if (type.HasGenericParameters)
{
countMethod = countMethod.MakeHostInstanceGeneric(type.GenericParameters.Select(gp => new GenericParameter(gp.Position, gp.Type, gp.Module)).ToArray());
copyToMethod = copyToMethod.MakeHostInstanceGeneric(type.GenericParameters.Select(gp => new GenericParameter(gp.Position, gp.Type, gp.Module)).ToArray());
}
var getMethod = new MethodDefinition("get_Items", MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName, itemArray);
var getMethodBody = getMethod.Body;
var localItems = new VariableDefinition("items", itemArray);
getMethodBody.Variables.Add(localItems);
getMethodBody.SimplifyMacros();
getMethodBody.Instructions.Add(Instruction.Create(OpCodes.Ldarg_0));
getMethodBody.Instructions.Add(Instruction.Create(OpCodes.Ldfld, fieldRef));
getMethodBody.Instructions.Add(Instruction.Create(OpCodes.Callvirt, countMethod));
getMethodBody.Instructions.Add(Instruction.Create(OpCodes.Newarr, itemType));
getMethodBody.Instructions.Add(Instruction.Create(OpCodes.Stloc, localItems));
getMethodBody.Instructions.Add(Instruction.Create(OpCodes.Ldarg_0));
getMethodBody.Instructions.Add(Instruction.Create(OpCodes.Ldfld, fieldRef));
getMethodBody.Instructions.Add(Instruction.Create(OpCodes.Ldloc, localItems));
getMethodBody.Instructions.Add(Instruction.Create(OpCodes.Ldloc, localItems));
getMethodBody.Instructions.Add(Instruction.Create(OpCodes.Ldlen));
getMethodBody.Instructions.Add(Instruction.Create(OpCodes.Conv_I4));
getMethodBody.Instructions.Add(Instruction.Create(OpCodes.Callvirt, copyToMethod));
getMethodBody.Instructions.Add(Instruction.Create(OpCodes.Ldloc, localItems));
getMethodBody.Instructions.Add(Instruction.Create(OpCodes.Ret));
getMethodBody.InitLocals = true;
getMethodBody.OptimizeMacros();
proxyType.Methods.Add(getMethod);
var property = new PropertyDefinition("Items", PropertyAttributes.None, itemArray);
property.GetMethod = getMethod;
var debuggerBrowsableAttribute = new CustomAttribute(ReferenceFinder.DebuggerBrowsableAttributeCtor);
debuggerBrowsableAttribute.ConstructorArguments.Add(new CustomAttributeArgument(ReferenceFinder.DebuggerBrowsableStateType, DebuggerBrowsableState.RootHidden));
property.CustomAttributes.Add(debuggerBrowsableAttribute);
proxyType.Properties.Add(property);
AddDebuggerTypeProxyAttribute(type, proxyType);
}
示例9: GetGenericInstanceType
private static GenericInstanceType GetGenericInstanceType(TypeDefinition type, Collection<GenericParameter> parameters)
{
var genericInstanceType = new GenericInstanceType(type);
foreach (var genericParameter in parameters)
{
genericInstanceType.GenericArguments.Add(genericParameter);
}
return genericInstanceType;
}
示例10: NewGenericInstanceTypeWithArgumentsFrom
private static GenericInstanceType NewGenericInstanceTypeWithArgumentsFrom(TypeReference referenceType, GenericInstanceType argumentSource)
{
GenericInstanceType replacementTypeReference = new GenericInstanceType(referenceType);
foreach (TypeReference argument in argumentSource.GenericArguments)
{
replacementTypeReference.GenericArguments.Add(argument);
}
return replacementTypeReference;
}
示例11: AsMethodOfGenericTypeInstance
public static MethodReference AsMethodOfGenericTypeInstance(this MethodDefinition methodDef, GenericInstanceType genericInstanceType)
{
if (!genericInstanceType.ElementType.IsEqualTo(methodDef.DeclaringType)) {
throw new ArgumentException("The generic instance type doesn't match the method's declaring type.", "genericInstanceType");
}
var methodRef = methodDef.Clone();
methodRef.DeclaringType = genericInstanceType;
return methodRef;
}
示例12: MemberResolver
public MemberResolver(TypeReference target, ModuleDefinition targetModule = null)
{
if (target == null) throw new ArgumentNullException("target");
Target = target;
Module = targetModule ?? Target.Module;
Source = Target.Resolve();
TargetWithArguments = Target as GenericInstanceType;
_map = new GenericParametersMap(Source, TargetWithArguments);
}
示例13: ConstructGenericType
private static GenericInstanceType ConstructGenericType(GenericContext context, TypeDefinition typeDefinition, IEnumerable<TypeReference> genericArguments)
{
GenericInstanceType type = new GenericInstanceType(typeDefinition);
foreach (TypeReference reference in genericArguments)
{
type.GenericArguments.Add(InflateType(context, reference));
}
return type;
}
示例14: GenericReferenceFor
private static TypeReference GenericReferenceFor(TypeReference type)
{
var instance = new GenericInstanceType(type);
foreach (var param in type.GenericParameters)
{
instance.GenericArguments.Add(param);
}
return instance;
}
示例15: 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;
}