Represents configuration options for compiling a model to a standalone assembly.
コード例 #1
0
ファイル: AssemblyGen.cs プロジェクト: AqlaSolutions/runsharp
        void Initialize(Universe universe, string assemblyName, AssemblyBuilderAccess access, CompilerOptions options, ITypeMapper typeMapper = null)
        {
            if (universe == null) throw new ArgumentNullException(nameof(universe));
            if (options == null) throw new ArgumentNullException(nameof(options));
            _compilerOptions = options;
            if (typeMapper == null)
#if FEAT_IKVM
                typeMapper = new TypeMapper(universe);
#else
                typeMapper = new TypeMapper();
#endif
            ExpressionFactory = new ExpressionFactory(typeMapper);
            StaticFactory = new StaticFactory(typeMapper);

#if SILVERLIGHT
            bool save = false;
#else
            bool save = (access & AssemblyBuilderAccess.Save) != 0;
#endif
            string path = options.OutputPath;
            if (path == null && save) throw new ArgumentNullException("options.OutputPath");

            Universe = universe;

            TypeMapper = typeMapper;
            _access = access;

            if (Helpers.IsNullOrEmpty(assemblyName))
            {
                if (save) throw new ArgumentNullException(nameof(assemblyName));
                assemblyName = Guid.NewGuid().ToString();
            }
            
            string moduleName = path == null ? assemblyName : assemblyName + Path.GetExtension(path);

            _fileName = path;

            AssemblyName an = new AssemblyName();
            an.Name = assemblyName;

            AssemblyBuilder =
#if !SILVERLIGHT
                path != null ? Universe.DefineDynamicAssembly(an, access, Path.GetDirectoryName(path)) :
#endif
                    Universe.DefineDynamicAssembly(an, access);
#if FEAT_IKVM
            if (!Helpers.IsNullOrEmpty(options.KeyFile))
            {
               AssemblyBuilder.__SetAssemblyKeyPair(new StrongNameKeyPair(File.OpenRead(options.KeyFile)));
            }
            else if (!Helpers.IsNullOrEmpty(options.KeyContainer))
            {
                AssemblyBuilder.__SetAssemblyKeyPair(new StrongNameKeyPair(options.KeyContainer));
            }
            else if (!Helpers.IsNullOrEmpty(options.PublicKey))
            {
                AssemblyBuilder.__SetAssemblyPublicKey(FromHex(options.PublicKey));
            }
            if (!Helpers.IsNullOrEmpty(options.ImageRuntimeVersion) && options.MetaDataVersion != 0)
            {
                AssemblyBuilder.__SetImageRuntimeVersion(options.ImageRuntimeVersion, options.MetaDataVersion);
            }
            ModuleBuilder = AssemblyBuilder.DefineDynamicModule(moduleName, path, options.SymbolInfo);
#else
            if (save)
            {
#if !SILVERLIGHT
                ModuleBuilder = AssemblyBuilder.DefineDynamicModule(moduleName, Path.GetFileName(path));
#else
                throw new NotSupportedException("Can't save on this platform");
#endif
            }
            else
                ModuleBuilder = AssemblyBuilder.DefineDynamicModule(moduleName);
#endif
        }
コード例 #2
0
ファイル: AssemblyGen.cs プロジェクト: AqlaSolutions/runsharp
        private void WriteAssemblyAttributes(CompilerOptions options, string assemblyName, AssemblyBuilder asm)
        {
            if (!Helpers.IsNullOrEmpty(options.TargetFrameworkName))
            {
                // get [TargetFramework] from mscorlib/equivalent and burn into the new assembly
                Type versionAttribType = null;
                try
                { // this is best-endeavours only
                    versionAttribType = TypeMapper.GetType("System.Runtime.Versioning.TargetFrameworkAttribute", TypeMapper.MapType(typeof(string)).Assembly);
                }
                catch { /* don't stress */ }
                if (versionAttribType != null)
                {
                    PropertyInfo[] props;
                    object[] propValues;
                    if (Helpers.IsNullOrEmpty(options.TargetFrameworkDisplayName))
                    {
                        props = new PropertyInfo[0];
                        propValues = new object[0];
                    }
                    else
                    {
                        props = new PropertyInfo[1] { versionAttribType.GetProperty("FrameworkDisplayName") };
                        propValues = new object[1] { options.TargetFrameworkDisplayName };
                    }
                    CustomAttributeBuilder builder = new CustomAttributeBuilder(
                        versionAttribType.GetConstructor(new Type[] { TypeMapper.MapType(typeof(string)) }),
                        new object[] { options.TargetFrameworkName },
                        props,
                        propValues);
                    asm.SetCustomAttribute(builder);
                }
            }

            // copy assembly:InternalsVisibleTo
            Type internalsVisibleToAttribType = null;
#if !FX11
            try
            {
                internalsVisibleToAttribType = TypeMapper.MapType(typeof(System.Runtime.CompilerServices.InternalsVisibleToAttribute));
            }
            catch { /* best endeavors only */ }
#endif
            if (internalsVisibleToAttribType != null)
            {
                List<string> internalAssemblies = new List<string>();
                List<Assembly> consideredAssemblies = new List<Assembly>();
                foreach (Type type in _types)
                {
                    Assembly assembly = type.Assembly;
                    if (consideredAssemblies.IndexOf(assembly) >= 0) continue;
                    consideredAssemblies.Add(assembly);

                    AttributeMap[] assemblyAttribsMap = AttributeMap.Create(TypeMapper, assembly);
                    for (int i = 0; i < assemblyAttribsMap.Length; i++)
                    {

                        if (assemblyAttribsMap[i].AttributeType != internalsVisibleToAttribType) continue;

                        object privelegedAssemblyObj;
                        assemblyAttribsMap[i].TryGet("AssemblyName", out privelegedAssemblyObj);
                        string privelegedAssemblyName = (string)privelegedAssemblyObj;
                        if (privelegedAssemblyName == assemblyName || Helpers.IsNullOrEmpty(privelegedAssemblyName)) continue; // ignore

                        if (internalAssemblies.IndexOf(privelegedAssemblyName) >= 0) continue; // seen it before
                        internalAssemblies.Add(privelegedAssemblyName);

                        CustomAttributeBuilder builder = new CustomAttributeBuilder(
                            internalsVisibleToAttribType.GetConstructor(new Type[] { TypeMapper.MapType(typeof(string)) }),
                            new object[] { privelegedAssemblyName });
                        asm.SetCustomAttribute(builder);
                    }
                }
            }
        }
コード例 #3
0
ファイル: AssemblyGen.cs プロジェクト: AqlaSolutions/runsharp
 public AssemblyGen(string assemblyName, CompilerOptions options)
     : this(new Universe(), assemblyName, options)
 {
 }
コード例 #4
0
ファイル: AssemblyGen.cs プロジェクト: AqlaSolutions/runsharp
        public AssemblyGen(Universe universe, string assemblyName, CompilerOptions options, ITypeMapper typeMapper = null)
	    {
	        Initialize(universe, assemblyName, AssemblyBuilderAccess.Save, options, typeMapper);
	    }
コード例 #5
0
ファイル: AssemblyGen.cs プロジェクト: AqlaSolutions/runsharp
        public AssemblyGen(string name, CompilerOptions options, ITypeMapper typeMapper = null)
        {
            Initialize(AppDomain.CurrentDomain, name,
#if !SILVERLIGHT
                !Helpers.IsNullOrEmpty(options.OutputPath) ? AssemblyBuilderAccess.RunAndSave : 
#endif
                AssemblyBuilderAccess.Run, 
                options, typeMapper);

        }
コード例 #6
0
ファイル: AssemblyGen.cs プロジェクト: AqlaSolutions/runsharp
 public AssemblyGen(AppDomain domain, string name, CompilerOptions options, ITypeMapper typeMapper = null)
 {
     Initialize(domain, name, AssemblyBuilderAccess.Run, options, typeMapper);
 }
コード例 #7
0
        private void WriteAssemblyAttributes(CompilerOptions options, string assemblyName, AssemblyBuilder asm)
        {
            if (!Helpers.IsNullOrEmpty(options.TargetFrameworkName))
            {
                // get [TargetFramework] from mscorlib/equivalent and burn into the new assembly
                Type versionAttribType = null;
                try
                { // this is best-endeavours only
                    versionAttribType = TypeMapper.GetType("System.Runtime.Versioning.TargetFrameworkAttribute", TypeMapper.MapType(typeof(string)).Assembly);
                }
                catch { /* don't stress */ }
                if (versionAttribType != null)
                {
                    PropertyInfo[] props;
                    object[]       propValues;
                    if (Helpers.IsNullOrEmpty(options.TargetFrameworkDisplayName))
                    {
                        props      = new PropertyInfo[0];
                        propValues = new object[0];
                    }
                    else
                    {
                        props = new PropertyInfo[1] {
                            versionAttribType.GetProperty("FrameworkDisplayName")
                        };
                        propValues = new object[1] {
                            options.TargetFrameworkDisplayName
                        };
                    }
                    CustomAttributeBuilder builder = new CustomAttributeBuilder(
                        versionAttribType.GetConstructor(new Type[] { TypeMapper.MapType(typeof(string)) }),
                        new object[] { options.TargetFrameworkName },
                        props,
                        propValues);
                    asm.SetCustomAttribute(builder);
                }
            }

            // copy assembly:InternalsVisibleTo
            Type internalsVisibleToAttribType = null;

#if !FX11
            try
            {
                internalsVisibleToAttribType = TypeMapper.MapType(typeof(System.Runtime.CompilerServices.InternalsVisibleToAttribute));
            }
            catch { /* best endeavors only */ }
#endif
            if (internalsVisibleToAttribType != null)
            {
                List <string>   internalAssemblies   = new List <string>();
                List <Assembly> consideredAssemblies = new List <Assembly>();
                foreach (Type type in _types)
                {
                    Assembly assembly = type.Assembly;
                    if (consideredAssemblies.IndexOf(assembly) >= 0)
                    {
                        continue;
                    }
                    consideredAssemblies.Add(assembly);

                    AttributeMap[] assemblyAttribsMap = AttributeMap.Create(TypeMapper, assembly);
                    for (int i = 0; i < assemblyAttribsMap.Length; i++)
                    {
                        if (assemblyAttribsMap[i].AttributeType != internalsVisibleToAttribType)
                        {
                            continue;
                        }

                        object privelegedAssemblyObj;
                        assemblyAttribsMap[i].TryGet("AssemblyName", out privelegedAssemblyObj);
                        string privelegedAssemblyName = (string)privelegedAssemblyObj;
                        if (privelegedAssemblyName == assemblyName || Helpers.IsNullOrEmpty(privelegedAssemblyName))
                        {
                            continue;                                                                                          // ignore
                        }
                        if (internalAssemblies.IndexOf(privelegedAssemblyName) >= 0)
                        {
                            continue;                                                          // seen it before
                        }
                        internalAssemblies.Add(privelegedAssemblyName);

                        CustomAttributeBuilder builder = new CustomAttributeBuilder(
                            internalsVisibleToAttribType.GetConstructor(new Type[] { TypeMapper.MapType(typeof(string)) }),
                            new object[] { privelegedAssemblyName });
                        asm.SetCustomAttribute(builder);
                    }
                }
            }
        }
コード例 #8
0
        void Initialize(Universe universe, string assemblyName, AssemblyBuilderAccess access, CompilerOptions options, ITypeMapper typeMapper = null)
        {
            if (universe == null)
            {
                throw new ArgumentNullException(nameof(universe));
            }
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }
            _compilerOptions = options;
            if (typeMapper == null)
#if FEAT_IKVM
            { typeMapper = new TypeMapper(universe); }
#else
            { typeMapper = new TypeMapper(); }
#endif
            ExpressionFactory = new ExpressionFactory(typeMapper);
            StaticFactory     = new StaticFactory(typeMapper);

#if SILVERLIGHT
            bool save = false;
#elif NET5_0 || NETSTANDARD
            bool save = options.OutputPath != null;
#else
            bool save = (access & AssemblyBuilderAccess.Save) != 0;
#endif
            string path = options.OutputPath;
            if (path == null && save)
            {
                throw new ArgumentNullException("options.OutputPath");
            }

            Universe = universe;

            TypeMapper = typeMapper;

            _access = access;

            if (Helpers.IsNullOrEmpty(assemblyName))
            {
                if (save)
                {
                    throw new ArgumentNullException(nameof(assemblyName));
                }
                assemblyName = Guid.NewGuid().ToString();
            }

            string moduleName = path == null ? assemblyName : assemblyName + Path.GetExtension(path);

            _fileName = path;

            AssemblyName an = new AssemblyName();
            an.Name = assemblyName;

            AssemblyBuilder =
#if NET5_0 || NETSTANDARD
                System.Reflection.Emit.AssemblyBuilder.DefineDynamicAssembly(an, access);
#else
#if !SILVERLIGHT
                path != null?Universe.DefineDynamicAssembly(an, access, Path.GetDirectoryName(path)) :
#endif
                Universe.DefineDynamicAssembly(an, access);
#endif
#if FEAT_IKVM
            if (!Helpers.IsNullOrEmpty(options.KeyFile))
            {
                AssemblyBuilder.__SetAssemblyKeyPair(new StrongNameKeyPair(File.OpenRead(options.KeyFile)));
            }
            else if (!Helpers.IsNullOrEmpty(options.KeyContainer))
            {
                AssemblyBuilder.__SetAssemblyKeyPair(new StrongNameKeyPair(options.KeyContainer));
            }
            else if (!Helpers.IsNullOrEmpty(options.PublicKey))
            {
                AssemblyBuilder.__SetAssemblyPublicKey(FromHex(options.PublicKey));
            }
            if (!Helpers.IsNullOrEmpty(options.ImageRuntimeVersion) && options.MetaDataVersion != 0)
            {
                AssemblyBuilder.__SetImageRuntimeVersion(options.ImageRuntimeVersion, options.MetaDataVersion);
            }
            ModuleBuilder = AssemblyBuilder.DefineDynamicModule(moduleName, path, options.SymbolInfo);
#else
#if !NET5_0 && !SILVERLIGHT && !NETSTANDARD
            if (save)
            {
                ModuleBuilder = AssemblyBuilder.DefineDynamicModule(moduleName, Path.GetFileName(path));
            }
            else
#endif
            ModuleBuilder = AssemblyBuilder.DefineDynamicModule(moduleName);
#endif
        }
コード例 #9
0
 public AssemblyGen(Universe universe, string assemblyName, CompilerOptions options, ITypeMapper typeMapper = null)
 {
     Initialize(universe, assemblyName, AssemblyBuilderAccess.Save, options, typeMapper);
 }
コード例 #10
0
 public AssemblyGen(string assemblyName, CompilerOptions options)
     : this(new Universe(), assemblyName, options)
 {
 }
コード例 #11
0
 public AssemblyGen(AppDomain domain, string name, CompilerOptions options, ITypeMapper typeMapper = null)
 {
     Initialize(domain, name, AssemblyBuilderAccess.Run, options, typeMapper);
 }