本文整理汇总了C#中Mono.Cecil.GenericParameter类的典型用法代码示例。如果您正苦于以下问题:C# GenericParameter类的具体用法?C# GenericParameter怎么用?C# GenericParameter使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
GenericParameter类属于Mono.Cecil命名空间,在下文中一共展示了GenericParameter类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Resolve
/// <summary>
/// Resolve the given generic parameter to a generic argument.
/// </summary>
public TypeReference Resolve(GenericParameter gp)
{
var owner = gp.Owner as TypeReference;
if (owner == null)
return gp; // Do not resolve generic method parameters for now
// Try to resolve the owner
var ownerDef = owner.GetElementType().Resolve();
if (ownerDef == null)
return gp;
// Try to find the owner in our map
var map = GetTypeToGit();
GenericInstanceType git;
if (!map.TryGetValue(ownerDef, out git))
{
// No mapping found
return gp;
}
// Replace with the given generic argument
var result = git.GenericArguments[gp.Position];
var resultAsGp = result as GenericParameter;
return (resultAsGp != null) ? Resolve(resultAsGp) : result;
}
示例2: Clone
/// <summary>
/// Clones the specified generic parameter.
/// </summary>
/// <param name="genericParameter">The generic parameter.</param>
/// <param name="methodDefinition">The method definition.</param>
/// <returns></returns>
public static GenericParameter Clone(this GenericParameter genericParameter, MethodDefinition methodDefinition)
{
var newGenericParameter = new GenericParameter(methodDefinition);
newGenericParameter.Attributes = genericParameter.Attributes;
newGenericParameter.Name = genericParameter.Name;
return newGenericParameter;
}
示例3: ILGenericParameter
/// <summary>
/// Default ctor
/// </summary>
public ILGenericParameter(XModule module, GenericParameter p) :
base(module)
{
this.p = p;
constraints = new Lazy<XTypeReference[]>(()=>
p.Constraints.Select(k=>AsTypeReference(module, k)).ToArray());
}
示例4: GetIndexOfGenericParameter
public static int GetIndexOfGenericParameter(MethodDefinition method, GenericParameter parameter)
{
if (parameter.IsGenericParameter)
{
// Genetic parameter has origin on method?
for (var index = 0; index < method.GenericParameters.Count; index++)
{
if (method.GenericParameters[index].MetadataToken == parameter.MetadataToken)
{
return index;
}
}
// Genetic parameter has origin on declared type?
for (var index = 0; index < method.DeclaringType.GenericParameters.Count; index++)
{
if (method.DeclaringType.GenericParameters[index].MetadataToken ==
parameter.MetadataToken)
{
return index;
}
}
}
throw new InvalidOperationException("Could not find generic parameter.");
}
示例5: GenerateTypeA
private TypeDefinition GenerateTypeA(AssemblyDefinition assembly)
{
var objType = assembly.MainModule.Import(typeof (object));
// define base class
var aType = new TypeDefinition("A", "", TypeAttributes.Class, objType);
assembly.MainModule.Types.Add(aType);
var typeParameter = new GenericParameter("T", aType);
aType.GenericParameters.Add(typeParameter);
var baseMethod = new MethodDefinition("Get",
MethodAttributes.Assem | MethodAttributes.Virtual |
MethodAttributes.NewSlot, typeParameter);
aType.Methods.Add(baseMethod);
baseMethod.DeclaringType = aType;
var local = new VariableDefinition(typeParameter);
baseMethod.Body.Variables.Add(local);
var cil = baseMethod.Body.CilWorker;
cil.Emit(OpCodes.Ldloca, local);
cil.Emit(OpCodes.Initobj, typeParameter);
cil.Emit(OpCodes.Ldloc, local);
cil.Emit(OpCodes.Ret);
return aType;
}
开发者ID:vestild,项目名称:nemerle,代码行数:27,代码来源:Cecil.GenericBaseClassAndOverrideOfNonPublicVirtualMethod.cs
示例6: Visit
public override TypeReference Visit(GenericParameter type)
{
TypeReference result;
if (genericTypeMapping.TryGetValue(type, out result))
return result;
return base.Visit(type);
}
示例7: HandleOwnerlessInvalidILCode
private TypeReference HandleOwnerlessInvalidILCode(GenericParameter genericParameter)
{
if (((genericParameter.Type == GenericParameterType.Method) && (this._typeDefinitionContext != null)) && (genericParameter.Position < this._typeDefinitionContext.GenericArguments.Count))
{
return this._typeDefinitionContext.GenericArguments[genericParameter.Position];
}
return genericParameter.Module.TypeSystem.Object;
}
示例8: GetGenericParameter
public TypeReference GetGenericParameter(GenericParameter genericParamter, IGenericParameterProvider[] paramProviders)
{
#if GENERIC
IGenericParameterProvider context = GetContext(genericParamter, paramProviders);
return context.GenericParameters[genericParamter.Position];
#else
return type;
#endif
}
示例9: CreateConstraint
static TypeReference CreateConstraint (string @namespace, string name, GenericParameter parameter)
{
return new TypeReference (
@namespace,
name,
parameter.Module,
parameter.Module.AssemblyReferences.First<AssemblyNameReference> (a => a.Name == "mscorlib"),
false);
}
示例10: AddConstraintsFromType
static void AddConstraintsFromType(ITypeParameter tp, GenericParameter g)
{
foreach (TypeReference constraint in g.Constraints) {
if (tp.Method != null) {
tp.Constraints.Add(CreateType(tp.Class.ProjectContent, tp.Method, constraint));
} else {
tp.Constraints.Add(CreateType(tp.Class.ProjectContent, tp.Class, constraint));
}
}
}
示例11: CreateGenericConstraints
private List<GenericConstraint> CreateGenericConstraints(GenericParameter genericParameter)
{
List<GenericConstraint> result = null;
if ((genericParameter.Attributes & GenericParameterAttributes.ReferenceTypeConstraint) == GenericParameterAttributes.ReferenceTypeConstraint)
{
result = new List<GenericConstraint>();
result.Add(new BuiltInGenericConstraint(BuiltInGenericConstraintsTypes.Class));
}
else if ((genericParameter.Attributes & GenericParameterAttributes.NotNullableValueTypeConstraint) == GenericParameterAttributes.NotNullableValueTypeConstraint)
{
result = new List<GenericConstraint>();
result.Add(new BuiltInGenericConstraint(BuiltInGenericConstraintsTypes.Struct));
}
ConstraintCollection baseOrInterfaceConstraints = genericParameter.Constraints;
if (baseOrInterfaceConstraints != null)
{
if (result == null) { result = new List<GenericConstraint>(); }
for (int i = 0; i < baseOrInterfaceConstraints.Count; i++)
{
TypeReference baseTypeOrInterface = baseOrInterfaceConstraints[i];
if (baseTypeOrInterface.FullName == "System.ValueType")
{
continue;
}
if (Utils.IsGenericParameter(baseTypeOrInterface))
{
result.Add(new NakedTypeConstraint(baseTypeOrInterface.Name));
}
else
{
string[] readableForms = Tools.GetHumanReadableForms(baseTypeOrInterface);
result.Add(new TypeGenericConstraint(readableForms[0]));
}
}
}
if ((genericParameter.Attributes & GenericParameterAttributes.DefaultConstructorConstraint) == GenericParameterAttributes.DefaultConstructorConstraint
&& (genericParameter.Attributes & GenericParameterAttributes.NotNullableValueTypeConstraint) == 0)
{
if (result == null) { result = new List<GenericConstraint>(); }
result.Add(new BuiltInGenericConstraint(BuiltInGenericConstraintsTypes.New));
}
return result;
}
示例12: GenericParameterEquals
public static bool GenericParameterEquals(GenericParameter genericParameter, GenericParameter genericParameter2)
{
if (genericParameter.Position != genericParameter.Position)
{
return false;
}
if (!TypeReferenceEquals(genericParameter, genericParameter2, false))
{
return false;
}
return true; // TODO: Implement this properly
}
示例13: CloneGenericParameterProperties
private static void CloneGenericParameterProperties(GenericParameter genericParameter,
GenericParameter newGenericParameter)
{
newGenericParameter.Attributes = genericParameter.Attributes;
genericParameter.Constraints.ForEach(gp => newGenericParameter.Constraints.Add(gp));
genericParameter.CustomAttributes.ForEach(ca => newGenericParameter.CustomAttributes.Add(ca));
newGenericParameter.DeclaringType = genericParameter.DeclaringType;
genericParameter.GenericParameters.ForEach(gp => newGenericParameter.GenericParameters.Add(gp));
newGenericParameter.HasDefaultConstructorConstraint = genericParameter.HasDefaultConstructorConstraint;
newGenericParameter.IsContravariant = genericParameter.IsContravariant;
newGenericParameter.IsCovariant = genericParameter.IsCovariant;
newGenericParameter.IsNonVariant = genericParameter.IsNonVariant;
}
示例14: GetEnumType
TypeReference GetEnumType(CustomAttribute attribute, GenericParameter parameter)
{
if (attribute.HasConstructorArguments)
{
var typeReference = (TypeReference) attribute.ConstructorArguments[0].Value;
if (!typeReference.IsEnumType())
{
var message = $"The type '{typeReference.FullName}' is not an enum type. Only enum types are permitted in an EnumConstraintAttribute.";
throw new WeavingException(message);
}
return typeReference;
}
return CreateConstraint("System", "Enum", parameter);
}
示例15: CollectConstraints
private static string CollectConstraints(GenericParameter param)
{
List<string> constraints = new List<string>();
if (param.HasDefaultConstructorConstraint)
{
constraints.Add("new()");
}
if(param.HasReferenceTypeConstraint)
{
constraints.Add("class");
}
constraints.AddRange(param.Constraints.Select(constraint => constraint.ToString()));
return string.Join(", ", constraints.ToArray());
}