本文整理汇总了C#中Mono.Cecil.TypeReference.GetElementType方法的典型用法代码示例。如果您正苦于以下问题:C# TypeReference.GetElementType方法的具体用法?C# TypeReference.GetElementType怎么用?C# TypeReference.GetElementType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Mono.Cecil.TypeReference
的用法示例。
在下文中一共展示了TypeReference.GetElementType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Process
/// <summary>
/// Transform open generic types to closed instantiation using context information.
/// As an example, if B{T} inherits from A{T}, running it with B{C} as context and A{B.T} as type, ti will return A{C}.
/// </summary>
public static TypeReference Process(TypeReference context, TypeReference type)
{
if (type == null)
return null;
var genericInstanceTypeContext = context as GenericInstanceType;
if (genericInstanceTypeContext == null)
return type;
if (genericInstanceTypeContext.ContainsGenericParameter())
return type;
// Build dictionary that will map generic type to their real implementation type
var resolvedType = context.Resolve();
var genericTypeMapping = new Dictionary<TypeReference, TypeReference>();
for (int i = 0; i < resolvedType.GenericParameters.Count; ++i)
{
var genericParameter = context.GetElementType().Resolve().GenericParameters[i];
genericTypeMapping.Add(genericParameter, genericInstanceTypeContext.GenericArguments[i]);
}
var visitor = new ResolveGenericsVisitor(genericTypeMapping);
var result = visitor.VisitDynamic(type);
// Make sure type is closed now
if (result.ContainsGenericParameter())
throw new InvalidOperationException("Unsupported generic resolution.");
return result;
}
示例2: GetSerializer
public virtual TypeReference GetSerializer(TypeReference objectType)
{
// Check if objectType matches genericSerializableType.
// Note: Not perfectly valid but hopefully it should be fast enough.
if (objectType.IsGenericInstance && checkInterfaces)
{
if (objectType.GetElementType().Resolve().Interfaces.Any(x => x.IsGenericInstance && x.GetElementType().FullName == genericSerializableType.FullName))
return CreateSerializer(objectType);
}
if (objectType.IsGenericInstance && objectType.GetElementType().FullName == genericSerializableType.FullName)
return CreateSerializer(objectType);
return null;
}
示例3: create
public string create(TypeReference typeRef)
{
if (typeRef.IsGenericInstance) {
var git = (GenericInstanceType)typeRef;
if (git.ElementType.FullName == "System.Nullable`1" &&
git.GenericArguments.Count == 1 && git.GenericArguments[0] != null) {
typeRef = git.GenericArguments[0];
}
}
string prefix = getPrefix(typeRef);
var elementType = typeRef.GetElementType();
if (elementType is GenericParameter)
return genericParamNameCreator.create();
NameCreator nc;
var typeFullName = typeRef.FullName;
if (typeNames.TryGetValue(typeFullName, out nc))
return nc.create();
var name = elementType.FullName;
var parts = name.Replace('/', '.').Split(new char[] { '.' });
var newName = parts[parts.Length - 1];
int tickIndex = newName.LastIndexOf('`');
if (tickIndex > 0)
newName = newName.Substring(0, tickIndex);
return addTypeName(typeFullName, newName, prefix).create();
}
示例4: Resolve
public static TypeDefinition Resolve(IAssemblyResolver resolver, TypeReference type)
{
type = type.GetElementType ();
var scope = type.Scope;
switch (scope.MetadataScopeType) {
case MetadataScopeType.AssemblyNameReference:
var assembly = resolver.Resolve ((AssemblyNameReference) scope);
if (assembly == null)
return null;
return GetType (assembly.MainModule, type);
case MetadataScopeType.ModuleDefinition:
return GetType ((ModuleDefinition) scope, type);
case MetadataScopeType.ModuleReference:
var modules = type.Module.Assembly.Modules;
var module_ref = (ModuleReference) scope;
for (int i = 0; i < modules.Count; i++) {
var netmodule = modules [i];
if (netmodule.Name == module_ref.Name)
return GetType (netmodule, type);
}
break;
}
throw new NotSupportedException ();
}
示例5: VisitTypeReference
public override void VisitTypeReference(TypeReference type)
{
if (type.GetElementType() is GenericParameter)
return;
var scope = GetAssemblyNameReference(type.Scope);
MapReference(scope, type);
}
示例6: IsStringOrSBuilder
private static bool IsStringOrSBuilder (TypeReference reference)
{
switch (reference.GetElementType ().FullName) {
case "System.String":
case "System.Text.StringBuilder":
return true;
default:
return false;
}
}
示例7: InstanceOfConditionInclude
/// <summary>
/// Default ctor
/// </summary>
public InstanceOfConditionInclude(TypeReference instanceOfCondition)
{
if (instanceOfCondition == null)
throw new ArgumentNullException("instanceOfCondition");
this.instanceOfCondition = instanceOfCondition.GetElementType().Resolve();
if (this.instanceOfCondition == null)
throw new CompilerException(string.Format("Cannot resolve InstanceOfCondition {0}", instanceOfCondition.FullName));
var attr = this.instanceOfCondition.GetDexOrJavaImportAttribute();
className = (attr != null) ? (string)attr.ConstructorArguments[0].Value : null;
}
示例8: DuplicateReturnValue
public static void DuplicateReturnValue(List<Instruction> instructions, TypeReference methodReturnType)
{
// Duplicate the stack (this should be the return value)
instructions.Add(Instruction.Create(OpCodes.Dup));
if (methodReturnType != null && methodReturnType.GetElementType().IsGenericParameter)
{
// Generic parameters must be boxed before access
instructions.Add(Instruction.Create(OpCodes.Box, methodReturnType));
}
}
示例9: EnumerateSubTypesFromSerializer
public IEnumerable<TypeReference> EnumerateSubTypesFromSerializer(TypeReference serializerType)
{
// Check if serializer type name matches
if (serializerType.IsGenericInstance && serializerType.GetElementType().FullName == genericSerializerTypeFullName)
{
if (genericSerializableType != null)
// Transforms genericSerializerType{T1, T2} into genericSerializableType{T1, T2}
return Enumerable.Repeat(genericSerializableType.MakeGenericType(((GenericInstanceType)serializerType).GenericArguments.ToArray()), 1);
else
// Transforms genericSerializerType{T1, T2} into T1, T2
return ((GenericInstanceType)serializerType).GenericArguments;
}
return null;
}
示例10: Resolve
public TypeDefinition Resolve (TypeReference type)
{
type = type.GetElementType ();
if (type is TypeDefinition)
return (TypeDefinition) type;
AssemblyNameReference reference = type.Scope as AssemblyNameReference;
if (reference != null) {
AssemblyDefinition assembly = Resolve (reference);
return assembly.MainModule.GetType (type.FullName);
}
ModuleDefinition module = type.Scope as ModuleDefinition;
if (module != null)
return module.GetType (type.FullName);
throw new NotImplementedException ();
}
示例11: AreEqual
public static bool AreEqual(TypeReference firstType, TypeReference secondType)
{
if (firstType.GetFriendlyFullName(null) == secondType.GetFriendlyFullName(null))
{
return true;
}
TypeReference firstTypeRef = firstType.GetElementType();
TypeReference secondTypeRef = secondType.GetElementType();
if (firstTypeRef.HasGenericParameters && secondTypeRef.HasGenericParameters)
{
if (firstTypeRef.FullName == secondTypeRef.FullName &&
firstTypeRef.GenericParameters.Count == secondTypeRef.GenericParameters.Count)
{
return true;
}
}
return false;
}
示例12: GetMonoEmbeddedFullTypeNameFor
private string GetMonoEmbeddedFullTypeNameFor(TypeReference type)
{
TypeSpecification typeSpecification = type as TypeSpecification;
string fullName;
if (typeSpecification != null && typeSpecification.IsRequiredModifier)
{
fullName = typeSpecification.ElementType.FullName;
}
else
{
if (type.IsRequiredModifier)
{
fullName = type.GetElementType().FullName;
}
else
{
fullName = type.FullName;
}
}
return fullName.Replace('/', '+').Replace('<', '[').Replace('>', ']');
}
示例13: create
public string create(TypeReference typeRef)
{
if (typeRef.IsGenericInstance) {
var git = (GenericInstanceType)typeRef;
if (git.ElementType.FullName == "System.Nullable`1" &&
git.GenericArguments.Count == 1 && git.GenericArguments[0] != null) {
typeRef = git.GenericArguments[0];
}
}
string prefix = getPrefix(typeRef);
var elementType = typeRef.GetElementType();
if (elementType is GenericParameter)
return genericParamNameCreator.create();
NameCreator nc;
var typeFullName = typeRef.FullName;
if (typeNames.TryGetValue(typeFullName, out nc))
return nc.create();
var fullName = elementType.FullName;
string shortName;
var dict = prefix == "" ? fullNameToShortName : fullNameToShortNamePrefix;
if (!dict.TryGetValue(fullName, out shortName)) {
fullName = fullName.Replace('/', '.');
int index = fullName.LastIndexOf('.');
shortName = index > 0 ? fullName.Substring(index + 1) : fullName;
index = shortName.LastIndexOf('`');
if (index > 0)
shortName = shortName.Substring(0, index);
}
return addTypeName(typeFullName, shortName, prefix).create();
}
示例14: resolve
static Type resolve(TypeReference typeReference)
{
if (typeReference == null)
return null;
var elemType = typeReference.GetElementType();
var resolver = getAssemblyResolver(elemType);
var resolvedType = resolver.resolve(elemType);
if (resolvedType != null)
return fixType(typeReference, resolvedType);
throw new ApplicationException(string.Format("Could not resolve type {0} ({1:X8}) in assembly {2}", typeReference, typeReference.MetadataToken.ToUInt32(), resolver));
}
示例15: TypesAreAssignable
public static bool TypesAreAssignable(TypeReference target, TypeReference source)
{
bool result;
// All values are assignable to object
if (target.FullName == "System.Object")
return true;
int targetDepth, sourceDepth;
if (TypesAreEqual(FullyDereferenceType(target, out targetDepth), FullyDereferenceType(source, out sourceDepth))) {
if (targetDepth == sourceDepth)
return true;
}
// Complex hack necessary because System.Array and T[] do not implement IEnumerable<T>
var targetGit = target as GenericInstanceType;
if (
(targetGit != null) &&
(targetGit.Name == "IEnumerable`1") &&
source.IsArray &&
(targetGit.GenericArguments.FirstOrDefault() == source.GetElementType())
)
return true;
var cacheKey = new Tuple<string, string>(target.FullName, source.FullName);
if (TypeAssignabilityCache.TryGetValue(cacheKey, out result))
return result;
var dSource = GetTypeDefinition(source);
if (dSource == null)
result = false;
else if (TypesAreEqual(target, dSource))
result = true;
else if ((dSource.BaseType != null) && TypesAreAssignable(target, dSource.BaseType))
result = true;
else {
foreach (var iface in dSource.Interfaces) {
if (TypesAreAssignable(target, iface)) {
result = true;
break;
}
}
}
TypeAssignabilityCache[cacheKey] = result;
return result;
}