/// <summary>
 /// Import framework options from an existing type
 /// </summary>
 public void SetFrameworkOptions(Type from, ITypeMapper mapper)
 {
     if (@from == null)
     {
         throw new ArgumentNullException(nameof(@from));
     }
     AttributeMap[] attribs = AttributeMap.Create(mapper, @from.Assembly);
     foreach (AttributeMap attrib in attribs)
     {
         if (attrib.AttributeType.FullName == "System.Runtime.Versioning.TargetFrameworkAttribute")
         {
             object tmp;
             if (attrib.TryGet("FrameworkName", out tmp))
             {
                 TargetFrameworkName = (string)tmp;
             }
             if (attrib.TryGet("FrameworkDisplayName", out tmp))
             {
                 TargetFrameworkDisplayName = (string)tmp;
             }
             break;
         }
     }
 }
Beispiel #2
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);
                    }
                }
            }
        }