Esempio n. 1
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. 2
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. 3
0
        private static void ctx_CustomAttributeDataLoadEvent(object sender, CustomAttributeDataEventArgs e)
        {
            IEnumerable <System.Reflection.CustomAttributeData> attrs;

            if (e.Member != null)
            {
                attrs = e.Member.GetCustomAttributesData();
            }
            else if (e.Type != null)
            {
                attrs = e.Type.GetCustomAttributesData();
            }
            else if (e.Parameter != null)
            {
                attrs = e.Parameter.GetCustomAttributesData();
            }
            else if (e.Assembly != null)
            {
                attrs = e.Assembly.GetCustomAttributesData();
            }
            else if (e.Module != null)
            {
                attrs = e.Module.GetCustomAttributesData();
            }
            else
            {
                throw new ArgumentException("Custom attribute data event with no native member?");
            }

            e.CustomAttributeData = attrs.Select(attr => Tuple.Create(
                                                     attr.Constructor.NewWrapper(e.Context),
                                                     attr.ConstructorArguments.Select(cArg => CILCustomAttributeFactory.NewTypedArgument((cArg.ArgumentType.NewWrapperAsType(e.Context)), cArg.Value)),
                                                     attr.NamedArguments.Select(nArg => CILCustomAttributeFactory.NewNamedArgument((nArg.MemberInfo is System.Reflection.PropertyInfo ? (CILElementForNamedCustomAttribute)((System.Reflection.PropertyInfo)nArg.MemberInfo).NewWrapper(e.Context) : ((System.Reflection.FieldInfo)nArg.MemberInfo).NewWrapper(e.Context)), CILCustomAttributeFactory.NewTypedArgument(nArg.TypedValue.ArgumentType.NewWrapperAsType(e.Context), nArg.TypedValue.Value)))
                                                     ));
        }