Example #1
0
        private bool IsDelegate(Cecil.TypeDefinition cecilType)
        {
            Cecil.TypeReference baseType = cecilType.BaseType;

            if (baseType == null)
            {
                return(false);
            }

            Cecil.IMetadataScope coreLibrary = cecilType.Module.TypeSystem.CoreLibrary;

            return(coreLibrary.Equals(baseType.Scope) && baseType.Namespace == "System" && baseType.Name == "MulticastDelegate");
        }
        internal static string AssemblyNameofType(TypeReference tref)
        {
            string lOwner;

            Mono.Cecil.IMetadataScope imds = (IMetadataScope)tref.Scope;
            AssemblyNameReference     asr  = imds as AssemblyNameReference;

            if (asr == null)
            {
                ModuleDefinition md = imds as ModuleDefinition;
                lOwner = md.Assembly.Name.FullName;
            }
            else
            {
                lOwner = asr.FullName;
            }
            return(lOwner);
        }
Example #3
0
        private Cecil.TypeReference TypeReference(AnalysisNet.Types.IBasicType basicType)
        {
            if (typesCache.TryGetValue(basicType, out Cecil.TypeReference cecilTypeReference))
            {
                return(cecilTypeReference);
            }

            Cecil.TypeReference platformType = TypeReferenceToPlatformType(basicType);
            if (platformType != null)
            {
                typesCache[basicType] = platformType;
                return(platformType);
            }

            string nmspace = basicType.ContainingNamespace;
            string name    = basicType.MetadataName();

            Cecil.ModuleDefinition module = ResolveModule(basicType);
            Cecil.IMetadataScope   scope  = module ?? ResolveScope(basicType);
            if (module == null && scope == null)
            {
                throw new NotImplementedException();
            }

            cecilTypeReference = new Cecil.TypeReference(nmspace, name, module, scope);
            if (basicType.TypeKind == AnalysisNet.Types.TypeKind.ValueType)
            {
                cecilTypeReference.IsValueType = true;
            }

            if (basicType.ContainingType != null)
            {
                cecilTypeReference.DeclaringType = TypeReference(basicType.ContainingType);
                cecilTypeReference.Namespace     = string.Empty;
            }

            if (basicType.GenericParameterCount > 0)
            {
                Cecil.GenericInstanceType instantiated = null;
                // should we add constraints?
                cecilTypeReference.CreateGenericParameters(basicType.GenericParameterCount);
                MapGenericParameters(cecilTypeReference, basicType);
                // call it before instantiating it
                cecilTypeReference = ImportTypeReference(cecilTypeReference);

                if (basicType.GenericArguments.Count == 0)
                {
                    instantiated = cecilTypeReference.MakeGenericInstanceType(cecilTypeReference.GenericParameters.ToArray());
                }
                else
                {
                    Cecil.TypeReference[] arguments = basicType.GenericArguments.Select(ga => TypeReference(ga)).ToArray();
                    instantiated = cecilTypeReference.MakeGenericInstanceType(arguments);
                }

                cecilTypeReference = instantiated;
            }
            else
            {
                cecilTypeReference = ImportTypeReference(cecilTypeReference);
            }

            typesCache[basicType] = cecilTypeReference;
            return(cecilTypeReference);
        }