本文整理汇总了C#中Dot42.CompilerLib.XModel.XTypeReference.GetElementType方法的典型用法代码示例。如果您正苦于以下问题:C# XTypeReference.GetElementType方法的具体用法?C# XTypeReference.GetElementType怎么用?C# XTypeReference.GetElementType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Dot42.CompilerLib.XModel.XTypeReference
的用法示例。
在下文中一共展示了XTypeReference.GetElementType方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: LoadTypeForGenericInstance
/// <summary>
/// Create an expression that loads the given type at runtime.
/// </summary>
private static AstExpression LoadTypeForGenericInstance(ISourceLocation seqp, MethodSource currentMethod,
XTypeReference type, AssemblyCompiler compiler, XTypeDefinition typeHelperType, XTypeSystem typeSystem,
TypeConversion typeConversion, XGenericInstanceType typeGenericArguments=null)
{
if (type.IsArray)
{
// Array type
var arrayType = (XArrayType)type;
// Load element type
var prefix = LoadTypeForGenericInstance(seqp, currentMethod, ((XArrayType)type).ElementType, compiler, typeHelperType, typeSystem, typeConversion);
// Convert to array type
if (arrayType.Dimensions.Count() == 1)
{
var giCreateArray = typeHelperType.Methods.Single(x => (x.Name == "Array") && (x.Parameters.Count == 1));
return new AstExpression(seqp, AstCode.Call, giCreateArray, prefix) { ExpectedType = typeSystem.Type };
}
else
{
var giCreateArray = typeHelperType.Methods.Single(x => (x.Name == "Array") && (x.Parameters.Count == 2));
var dimensionsExpr = new AstExpression(seqp, AstCode.Ldc_I4, arrayType.Dimensions.Count()) { ExpectedType = typeSystem.Int };
return new AstExpression(seqp, AstCode.Call, giCreateArray, prefix, dimensionsExpr) { ExpectedType = typeSystem.Type };
}
}
var gp = type as XGenericParameter;
if (gp != null)
{
AstExpression loadExpr;
if (gp.Owner is XTypeReference)
{
// Class type parameter
var owner = (XTypeReference)gp.Owner;
if (owner.GetElementType().Resolve().HasDexImportAttribute())
{
// Imported type
return new AstExpression(seqp, AstCode.TypeOf, typeSystem.Object) { ExpectedType = typeSystem.Type };
}
if (currentMethod.IsClassCtor)
{
// Class ctor's cannot have type information.
// Return Object instead
if(currentMethod.IsDotNet && !currentMethod.ILMethod.DeclaringType.HasSuppressMessageAttribute("StaticConstructorUsesGenericParameter"))
{
var msg = "Class (static) constructor of '{0}' tries to use generic parameter. This will always yield Object. " +
"You can suppress this warning with a [SuppressMessage(\"dot42\", \"StaticConstructorUsesGenericParameter\")] " +
"attribute on the class.";
if(seqp != null && seqp.Document != null)
DLog.Warning(DContext.CompilerCodeGenerator, seqp.Document, seqp.StartColumn,seqp.StartLine, msg, currentMethod.DeclaringTypeFullName);
else
DLog.Warning(DContext.CompilerCodeGenerator, msg, currentMethod.DeclaringTypeFullName);
}
return new AstExpression(seqp, AstCode.TypeOf, typeSystem.Object) { ExpectedType = typeSystem.Type };
}
loadExpr = currentMethod.IsStatic ?
LoadStaticClassGenericArgument(seqp, typeSystem, currentMethod.Method, gp.Position) :
LoadInstanceClassGenericArgument(seqp, typeSystem, currentMethod.Method.DeclaringType, gp.Position);
}
else
{
// Method type parameter
var owner = (XMethodReference)gp.Owner;
if (owner.GetElementMethod().Resolve().DeclaringType.HasDexImportAttribute())
{
// Imported type
return LoadTypeForGenericInstance(seqp, currentMethod, type.Module.TypeSystem.Object, compiler, typeHelperType, typeSystem, typeConversion);
}
loadExpr = LoadMethodGenericArgument(seqp, typeSystem, currentMethod.Method, gp.Position);
}
if (typeConversion == TypeConversion.EnsureRuntimeType)
return EnsureGenericRuntimeType(loadExpr, typeSystem, typeHelperType);
else
return loadExpr;
}
if (type is XTypeSpecification)
{
var typeSpec = (XTypeSpecification)type;
var git = type as XGenericInstanceType;
var baseType = LoadTypeForGenericInstance(seqp, currentMethod, typeSpec.ElementType, compiler, typeHelperType, typeSystem, typeConversion, git);
if (typeConversion != TypeConversion.EnsureTrueOrMarkerType || typeSpec.GetElementType().IsNullableT())
return baseType;
// Use the element type and make a generic proxy with the generic arguments.
var parameters = CreateGenericInstanceCallArguments(seqp, git, currentMethod, compiler);
if (parameters.Count == 1 && parameters[0].GetResultType().IsArray)
{
// array type call.
var method = typeHelperType.Methods.Single(m => m.Name == "GetGenericInstanceType" && m.Parameters.Count == 2 && m.Parameters[1].ParameterType.IsArray);
return new AstExpression(seqp, AstCode.Call, method, baseType, parameters[0]);
}
else
{
parameters.Insert(0, baseType);
var method = typeHelperType.Methods.Single(m => m.Name == "GetGenericInstanceType" && m.Parameters.Count == parameters.Count && !m.Parameters[1].ParameterType.IsArray);
//.........这里部分代码省略.........
示例2: GetGenericDefinitionAnnotationForType
public static Annotation GetGenericDefinitionAnnotationForType(XTypeReference xtype, bool forceTypeDefinition, AssemblyCompiler compiler, DexTargetPackage targetPackage)
{
var genericsDefAnnotationClass = compiler.GetDot42InternalType(InternalConstants.GenericDefinitionAnnotation)
.GetClassReference(targetPackage);
var annotation = new Annotation {Type = genericsDefAnnotationClass, Visibility = AnnotationVisibility.Runtime};
if (xtype.IsGenericInstance)
{
bool handled = false;
List<object> genericArguments = new List<object>();
if (xtype.GetElementType().IsNullableT())
{
// privitives and enums are represented by their marker classes.
// no annotation needed.
var argument = ((XGenericInstanceType) xtype).GenericArguments[0];
if (argument.IsEnum() || argument.IsPrimitive && !forceTypeDefinition)
{
return null;
}
// structs have marker classes.
var classRef = xtype.GetReference(targetPackage) as ClassReference;
var @class = classRef == null ? null : targetPackage.DexFile.GetClass(classRef.Fullname);
if (@class != null && @class.NullableMarkerClass != null)
{
annotation.Arguments.Add(new AnnotationArgument("GenericInstanceType", @class.NullableMarkerClass));
handled = true;
}
}
if (!handled)
{
foreach (var arg in ((XGenericInstanceType) xtype).GenericArguments)
{
if (arg.IsGenericParameter)
{
var gparm = (XGenericParameter) arg;
// TODO: if we wanted to annotate methods as well, we should differentiate
// between generic method arguments and generic type arguments.
genericArguments.Add(gparm.Position);
}
else if (arg.IsGenericInstance)
{
var giparm = GetGenericDefinitionAnnotationForType((XGenericInstanceType) arg, true,compiler, targetPackage);
genericArguments.Add(giparm);
}
else
{
genericArguments.Add(arg.GetReference(targetPackage));
}
}
annotation.Arguments.Add(new AnnotationArgument("GenericArguments", genericArguments.ToArray()));
}
}
else // generic parameter
{
var parm = (XGenericParameter) xtype;
annotation.Arguments.Add(new AnnotationArgument("GenericParameter", parm.Position));
}
if(forceTypeDefinition && annotation.Arguments.All(a => a.Name != "GenericInstanceType"))
annotation.Arguments.Add(new AnnotationArgument("GenericTypeDefinition", xtype.ElementType.GetReference(targetPackage)));
return annotation;
}
示例3: GetDefinition
/// <summary>
/// The syntax is experimental, and neither fully fixed nor officially defined.
/// </summary>
private static string GetDefinition(XTypeReference xtype, bool forceTypeDefinition, AssemblyCompiler compiler,
DexTargetPackage targetPackage)
{
StringBuilder s = new StringBuilder();
// TODO: reorganize, so that the syntax becomes clear.
bool setGenericInstanceType = false;
if (xtype.IsGenericInstance)
{
bool handled = false;
if (xtype.GetElementType().IsNullableT())
{
// privitives and enums are represented by their marker classes.
// no annotation needed.
var argument = ((XGenericInstanceType) xtype).GenericArguments[0];
if (!forceTypeDefinition && (argument.IsEnum() || argument.IsPrimitive))
{
return "";
}
// structs have marker classes.
var classRef = xtype.GetReference(targetPackage) as ClassReference;
var @class = classRef == null ? null : targetPackage.DexFile.GetClass(classRef.Fullname);
if (@class != null && @class.NullableMarkerClass != null)
{
s.Append("@");
s.Append(GetClassName(@class.NullableMarkerClass));
setGenericInstanceType = true;
handled = true;
}
}
if (!handled)
{
bool isFirst = true;
foreach (var arg in ((XGenericInstanceType) xtype).GenericArguments)
{
if (!isFirst) s.Append(",");
isFirst = false;
if (arg.IsGenericParameter)
{
var gparm = (XGenericParameter) arg;
// TODO: if we wanted to annotate methods as well, we should differentiate
// between generic method arguments and generic type arguments.
s.Append("!");
s.Append(gparm.Position);
}
else if (arg.IsGenericInstance)
{
var giparm = GetDefinition((XGenericInstanceType)arg, true, compiler, targetPackage);
s.Append("{");
s.Append(giparm);
s.Append("}");
}
else
{
s.Append(GetClassName(arg.GetReference(targetPackage)));
}
}
}
}
else // generic parameter
{
var parm = (XGenericParameter) xtype;
s.Append("!!");
s.Append(parm.Position);
}
if (forceTypeDefinition && !setGenericInstanceType)
{
string def = GetClassName(xtype.ElementType.GetReference(targetPackage));
s.Insert(0, def + "<");
s.Append(">");
}
return s.ToString();
}