Esempio n. 1
0
        /// <summary>
        /// Runs specified method.
        /// </summary>
        /// <param name="entryMethod">The entry method.</param>
        /// <returns>TestingAssembly.</returns>
        private TestingAssembly runWithAssemblyLoad(Func <string> entryMethod)
        {
            var assembly = new CILAssembly(GetType().Assembly.Location);

            return(AssemblyUtils.RunCIL(entryMethod)
                   .AddAssembly(assembly));
        }
Esempio n. 2
0
        public Boolean TryGetMappedAssembly(CILAssemblyName name, out CILAssembly assembly)
        {
            Lazy <CILAssembly> lazy;
            var retVal = this._assemblies.TryGetValue(name, out lazy);

            assembly = retVal ? lazy.Value : null;
            return(retVal);
        }
Esempio n. 3
0
 /// <summary>
 /// Adds a <see cref="TargetFrameworkAttribute"/> to the <see cref="CILAssembly"/> which has given framework information.
 /// This method should not be used when adding <see cref="TargetFrameworkAttribute"/> to Portable Class Libraries.
 /// </summary>
 /// <param name="assembly">The <see cref="CILAssembly"/>.</param>
 /// <param name="fwName">The name of the target framework. This will be passed directly as string argument to the attribute constructor.</param>
 /// <param name="fwDisplayName">The display name of the target framework.</param>
 /// <exception cref="NullReferenceException">If <paramref name="assembly"/> is <c>null</c>.</exception>
 public static void AddTargetFrameworkAttributeNative(this CILAssembly assembly, String fwName, String fwDisplayName)
 {
     assembly.AddCustomAttribute(
         ModuleReader.TARGET_FRAMEWORK_ATTRIBUTE_CTOR.NewWrapper(assembly.ReflectionContext),
         new CILCustomAttributeTypedArgument[] { CILCustomAttributeFactory.NewTypedArgument(ModuleReader.TARGET_FRAMEWORK_ATTRIBUTE_CTOR.GetParameters().First().ParameterType.NewWrapperAsType(assembly.ReflectionContext), fwName) },
         fwDisplayName == null ? null : new CILCustomAttributeNamedArgument[] { CILCustomAttributeFactory.NewNamedArgument(ModuleReader.TARGET_FRAMEWORK_ATTRIBUTE_NAMED_PROPERTY.NewWrapper(assembly.ReflectionContext), CILCustomAttributeFactory.NewTypedArgument((CILType)ModuleReader.TARGET_FRAMEWORK_ATTRIBUTE_NAMED_PROPERTY.NewWrapper(assembly.ReflectionContext).GetPropertyType(), fwDisplayName)) }
         );
 }
Esempio n. 4
0
    /// <summary>
    /// This extension method gets the public key token of the given <see cref="CILAssembly"/>.
    /// If the <see cref="CILAssemblyName.Flags"/> specify that the assembly name is not using full public key, then the <see cref="CILAssemblyName.PublicKey"/> is returned directly.
    /// Otherwise, the public key token is computed by using <see cref="CILReflectionContext.ComputePublicKeyToken"/> method.
    /// </summary>
    /// <param name="assembly">The <see cref="CILAssembly"/>.</param>
    /// <returns>The public key token of the given <see cref="CILAssembly"/>.</returns>
    public static Byte[] GetPublicKeyToken(this CILAssembly assembly)
    {
        var an     = assembly.Name;
        var result = an.PublicKey;

        return(an.Flags.IsFullPublicKey() && result != null && result.Length > 0 ?
               assembly.ReflectionContext.ComputePublicKeyToken(result) :
               result);
    }
Esempio n. 5
0
        public RuntimeTypeFactory(CILAssembly cilAssembly, CILModule cilModule)
        {
            var assemblyName    = new AssemblyName(cilAssembly.AssemblyName);
            var assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);

            _assemblyBuilder = assemblyBuilder;

            var moduleBuilder = _assemblyBuilder.DefineDynamicModule(cilModule.ModuleName);

            _moduleBuilder = moduleBuilder;
        }
    private static void ForceLoadAssembly(CILAssembly assembly)
    {
        var dummy1 = assembly.Name;
        var dummy2 = assembly.CustomAttributeData;
        var dummy3 = assembly.ForwardedTypeInfos;

        foreach (var mod in assembly.Modules)
        {
            ForceLoadModule(mod);
        }
    }
Esempio n. 7
0
        /// <inheritdoc />
        protected override void generate(EmitterBase emitter)
        {
            //first and only argument is parametric
            var resolvedArguments = CILAssembly.ResolveCustomAttributeArgument(_attribute.ConstructorArguments[0]) as object[];

            for (var i = 0; i < resolvedArguments.Length; ++i)
            {
                var argumentStorage = "arg" + i;
                var argumentValue   = resolvedArguments[i];

                emitter.AssignLiteral(argumentStorage, argumentValue);
            }
        }
Esempio n. 8
0
    /// <summary>
    /// Adds a <see cref="TargetFrameworkAttribute"/> to the <see cref="CILAssembly"/> which represents information for given target framework.
    /// </summary>
    /// <param name="assembly">The <see cref="CILAssembly"/>.</param>
    /// <param name="monikerInfo">The information about target framework, see <see cref="FrameworkMonikerInfo"/>.</param>
    /// <param name="monikerMapper">The assembly mapper helper, created by <see cref="E_CIL.CreateMapperForFrameworkMoniker"/> method, or by creating <see cref="EmittingArguments"/> by <see cref="EmittingArguments.CreateForEmittingWithMoniker"/> method and accessing its <see cref="EmittingArguments.AssemblyMapper"/> property.</param>
    /// <exception cref="NullReferenceException">If <paramref name="assembly"/> is <c>null</c>.</exception>
    /// <exception cref="ArgumentNullException">If <paramref name="monikerInfo"/> or <paramref name="monikerMapper"/> is <c>null</c>.</exception>
    public static void AddTargetFrameworkAttributeWithMonikerInfo(this CILAssembly assembly, FrameworkMonikerInfo monikerInfo, EmittingAssemblyMapper monikerMapper)
    {
        ArgumentValidator.ValidateNotNull("Moniker info", monikerInfo);
        ArgumentValidator.ValidateNotNull("Moniker mapper", monikerMapper);
        var targetFWType = (CILType)monikerMapper.MapTypeBase(ModuleReader.TARGET_FRAMEWORK_ATTRIBUTE_CTOR.DeclaringType.NewWrapper(assembly.ReflectionContext));
        var targetFWCtor = targetFWType.Constructors.First(ctor => ctor.Parameters.Count == 1 && ctor.Parameters[0].ParameterType is CILType && ((CILType)ctor.Parameters[0].ParameterType).TypeCode == CILTypeCode.String);
        var targetFWProp = targetFWType.DeclaredProperties.First(prop => prop.Name == ModuleReader.TARGET_FRAMEWORK_ATTRIBUTE_NAMED_PROPERTY.Name);
        var fwString     = monikerInfo.FrameworkName + ",Version=" + monikerInfo.FrameworkVersion;

        if (!String.IsNullOrEmpty(monikerInfo.ProfileName))
        {
            fwString += ",Profile=" + monikerInfo.ProfileName;
        }
        assembly.AddCustomAttribute(
            targetFWCtor,
            new CILCustomAttributeTypedArgument[] { CILCustomAttributeFactory.NewTypedArgument((CILType)targetFWCtor.Parameters[0].ParameterType, fwString) },
            new CILCustomAttributeNamedArgument[] { CILCustomAttributeFactory.NewNamedArgument(targetFWProp, CILCustomAttributeFactory.NewTypedArgument((CILType)targetFWProp.GetPropertyType(), monikerInfo.FrameworkDisplayName)) }
            );
    }
Esempio n. 9
0
 /// <summary>
 /// Tries to get PCL profile based on <see cref="TargetFrameworkAttribute"/> applied on <see cref="CILAssembly"/>.
 /// </summary>
 /// <param name="assembly">The <see cref="CILAssembly"/>.</param>
 /// <param name="fwName">This will contain the framework name, if attribute is successfully found, otherwise <c>null</c>.</param>
 /// <param name="fwVersion">This will contain the framework version, if attribute is successfully found, otherwise <c>null</c>.</param>
 /// <param name="fwProfile">This will contain the framework profile, if attribute is successfully found, otherwise <c>null</c>.</param>
 /// <remarks>
 /// This method works by detecting an custom attribute of type <c>System.Runtime.Versioning.TargetFrameworkAttribute</c> which contains one constructor argument of type <see cref="String"/>.
 /// Then the method tries to detect to parse the required information by assuming the string is in format <c>&lt;fwName&gt;,Version=&lt;fwVersion&gt;,Profile=&lt;fwProfile&gt;</c>, where version and profile information are optional.
 /// </remarks>
 public static void TryGetTargetFrameworkInfoBasedOnAttribute(this CILAssembly assembly, out String fwName, out String fwVersion, out String fwProfile)
 {
     fwName    = null;
     fwVersion = null;
     fwProfile = null;
     if (assembly != null)
     {
         foreach (var cd in assembly.CustomAttributeData)
         {
             if (String.Equals(cd.Constructor.DeclaringType.Name, "TargetFrameworkAttribute") &&
                 String.Equals(cd.Constructor.DeclaringType.Namespace, "System.Runtime.Versioning") &&
                 cd.ConstructorArguments.Count == 1 &&
                 cd.ConstructorArguments[0].Value is String)
             {
                 break;
             }
         }
     }
 }
Esempio n. 10
0
 internal CILModuleImpl(CILReflectionContextImpl ctx, Int32 anID, CILAssembly ass, String name)
     : base(ctx, CILElementKind.Module, anID, new LazyWithLock <ListProxy <CILCustomAttribute> >(() => ctx.CollectionsFactory.NewListProxy <CILCustomAttribute>()))
 {
     InitFields(
         ref this.name,
         ref this.assembly,
         ref this.types,
         ref this.moduleInitializer,
         ref this.associatedMSCorLib,
         ref this.typeNameCache,
         ref this.manifestResources,
         name,
         () => ass,
         () => ctx.CollectionsFactory.NewListProxy <CILType>(),
         this.BuildModuleInitializerType,
         this.LoadNativeMSCorLibModule,
         null,
         this
         );
 }
Esempio n. 11
0
 private TypeModuleIterator(string currentPath, CILAssembly assembly)
 {
     _currentPath = currentPath;
     _assembly    = assembly;
 }
Esempio n. 12
0
 internal TypeModuleIterator(CILAssembly assembly)
 {
     _assembly = assembly;
 }
Esempio n. 13
0
 /// <summary>
 /// Helper method to get framework moniker info directly from <see cref="CILAssembly"/>.
 /// </summary>
 /// <param name="loader">The <see cref="CILAssemblyLoader"/>.</param>
 /// <param name="assembly">The <see cref="CILAssembly"/>.</param>
 /// <returns>The <see cref="FrameworkMonikerInfo"/> for given <paramref name="assembly"/>.</returns>
 /// <remarks>
 /// <see cref="CILAssemblyLoader.GetFrameworkInfoFor"/> for exception information.
 /// </remarks>
 public static FrameworkMonikerInfo GetFrameworkInfoFor(this CILAssemblyLoader loader, CILAssembly assembly)
 {
     ArgumentValidator.ValidateNotNull("Assembly", assembly);
     return(loader.GetFrameworkInfoFor(assembly.MainModule));
 }
Esempio n. 14
0
        /// <summary>
        /// Tries to resolve given assembly name as if given module would reference it.
        /// </summary>
        /// <param name="thisModule">The <see cref="CILModule"/> loaded by this <see cref="CILAssemblyLoader"/>.</param>
        /// <param name="name">The <see cref="CILAssemblyName"/> of reference.</param>
        /// <param name="resolvedAssembly">This will hold the resolved assembly, if successful.</param>
        /// <returns><c>true</c> if successfully resolved reference; <c>false</c> otherwise.</returns>
        public Boolean TryResolveReference(CILModule thisModule, CILAssemblyName name, out CILAssembly resolvedAssembly)
        {
            String modPath         = null;
            String libAssemblyPath = null;
            var    retVal          = thisModule != null &&
                                     this._moduleResources.TryGetValue(thisModule, out modPath);

            if (retVal)
            {
                if (!this._callbacks.TryResolveAssemblyFilePath(modPath, name, out libAssemblyPath))
                {
                    // Have to deduce ourselves - most likely client assembly referencing system assembly
                    var fwInfo = this.GetMonikerInfoFor(modPath, this._loadingArgs[thisModule]);
                    if (!this._callbacks.TryGetFrameworkAssemblyPath(modPath, name, fwInfo.FrameworkName, fwInfo.FrameworkVersion, fwInfo.ProfileName, out libAssemblyPath))
                    {
                        // TODO event
                        libAssemblyPath = null;
                    }
                }
            }

            retVal           = libAssemblyPath != null;
            resolvedAssembly = retVal ? this.LoadModuleFrom(libAssemblyPath).Assembly : null;
            return(retVal);
        }
Esempio n. 15
0
 /// <summary>
 /// Helper method to try get <see cref="TypeForwardingInfo"/> based on type name and type namespace from <see cref="CILAssembly"/>.
 /// </summary>
 /// <param name="assembly">The <see cref="CILAssembly"/>.</param>
 /// <param name="typeName">The type name.</param>
 /// <param name="typeNamespace">The type namespace.</param>
 /// <param name="tfInfo">The resulting <see cref="TypeForwardingInfo"/>.</param>
 /// <returns><c>true</c> if <paramref name="assembly"/> contained a <see cref="TypeForwardingInfo"/> with given <paramref name="typeName"/> and <paramref name="typeNamespace"/> in its <see cref="CILAssembly.ForwardedTypeInfos"/> property; <c>false</c> otherwise.</returns>
 /// <exception cref="NullReferenceException">If <paramref name="assembly"/> is <c>null</c>.</exception>
 public static Boolean TryGetTypeForwarder(this CILAssembly assembly, String typeName, String typeNamespace, out TypeForwardingInfo tfInfo)
 {
     return(assembly.ForwardedTypeInfos.TryGetValue(Tuple.Create(typeName, typeNamespace), out tfInfo));
 }
Esempio n. 16
0
 /// <summary>
 /// Gets all defined types in <paramref name="assembly"/>, including nested types.
 /// </summary>
 /// <param name="assembly">The <see cref="CILAssembly"/> to get types from.</param>
 /// <returns>Enumerable for all defined types in <paramref name="assembly"/>.</returns>
 public static IEnumerable <CILType> GetAllDefinedTypes(this CILAssembly assembly)
 {
     return(assembly.Modules.SelectMany(m => m.DefinedTypes).SelectMany(t => t.AsBreadthFirstEnumerable(tt => tt.DeclaredNestedTypes)));
 }