public static bool IsAttributeDefined <TAttribute>(MemberInfo member, ReflectionOptions options) where TAttribute : Attribute
 {
     return(options switch
     {
         ReflectionOptions.Default => IsAttributeDefinedImpl(TypeReflectorHelper.GetReflector(member), typeof(TAttribute)),
         ReflectionOptions.Inherit => member.GetCustomAttributes <TAttribute>(true).Any(),
         _ => member.GetCustomAttributes <TAttribute>().Any()
     });
        public static string GetDescriptionOrDisplayName <T>(string memberName, ReflectionOptions refOptions = ReflectionOptions.Default)
        {
            if (string.IsNullOrWhiteSpace(memberName))
            {
                return(string.Empty);
            }
            var members = typeof(T).GetMembers();

            return(GetDescriptionOrDisplayNameImpl(members.FirstOrDefault(x => x.Name == memberName), refOptions, ReflectionAmbiguousOptions.IgnoreAmbiguous));
        }
 private static string GetDescriptionOrDisplayNameImpl(MemberInfo member, ReflectionOptions refOptions, ReflectionAmbiguousOptions ambOptions)
 {
     if (member is null)
     {
         return(string.Empty);
     }
     return(IsDescriptionDefined(member, refOptions)
         ? GetDescriptionImpl(member, refOptions, ambOptions)
         : GetDisplayNameImpl(member, refOptions, ambOptions));
 }
 private static string GetDescriptionImpl(ParameterInfo parameter, ReflectionOptions refOptions, ReflectionAmbiguousOptions ambOptions)
 {
     if (parameter is null)
     {
         return(string.Empty);
     }
     if (GetAttribute(parameter, typeof(DescriptionAttribute), refOptions, ambOptions) is DescriptionAttribute attribute)
     {
         return(attribute.Description);
     }
     if (GetAttribute(parameter, typeof(DisplayAttribute), refOptions, ambOptions) is DisplayAttribute displayAttribute)
     {
         return(displayAttribute.Description);
     }
     return(parameter.Name);
 }
 private static string GetDisplayNameImpl(MemberInfo member, ReflectionOptions refOptions, ReflectionAmbiguousOptions ambOptions)
 {
     if (member is null)
     {
         return(string.Empty);
     }
     if (GetAttribute(member, typeof(DisplayNameAttribute), refOptions, ambOptions) is DisplayNameAttribute displayNameAttribute)
     {
         return(displayNameAttribute.DisplayName);
     }
     if (GetAttribute(member, typeof(DisplayAttribute), refOptions, ambOptions) is DisplayAttribute displayAttribute)
     {
         return(displayAttribute.Name);
     }
     return(member.Name);
 }
Пример #6
0
        public RenameStep(RenamingOptions renamingOptions, ReflectionOptions reflectionOptions)
        {
            _renamingOptions = renamingOptions;
            _reflectionOptions = reflectionOptions;

            bool keepNamespaces = _renamingOptions.HasFlag(RenamingOptions.KeepNamespaces);
            INameGenerator nameGenerator = GetNameGenerator(new StringGenerator(ALPHABET), keepNamespaces);

            _pipeline = new Pipeline();
            _pipeline.AppendStep(new FillMethodImplTablesStep());
            _pipeline.AppendStep(new BuildRenameMapStep(nameGenerator));

            if (_renamingOptions.HasFlag(RenamingOptions.Reflection))
                _pipeline.AppendStep(new InjectReflectionMethodProxies(_reflectionOptions, keepNamespaces));

            if (_renamingOptions.HasFlag(RenamingOptions.SaveRenameMap))
                _pipeline.AppendStep(new SaveRenameMap());

            _pipeline.AppendStep(new RenameReferencesStep(_renamingOptions));
            _pipeline.AppendStep(new RenameDefinitionsStep(_renamingOptions));
        }
Пример #7
0
 public static bool IsAttributeDefined <TAttribute>(ParameterInfo parameter, ReflectionOptions options) where TAttribute : Attribute =>
 TypeReflections.IsAttributeDefined <TAttribute>(parameter, options);
Пример #8
0
 public static bool IsAttributeDefined(MemberInfo member, Type attributeType, ReflectionOptions options) =>
 TypeReflections.IsAttributeDefined(member, attributeType, options);
Пример #9
0
        public static void FillMap(TypeDefinition map, IDictionary<IMemberDefinition, string> renameData, ReflectionOptions options, bool keepNamespaces)
        {
            var constructor = map.Methods.Single(m => m.IsConstructor);

            //TODO: refactor - hardcodes strings
            var addTypeName = map.Methods.Single(m => m.Name == "AddTypeName");
            var addFieldName = map.Methods.Single(m => m.Name == "AddFieldName");
            var addEventName = map.Methods.Single(m => m.Name == "AddEventName");
            var addMethodName = map.Methods.Single(m => m.Name == "AddMethodName");
            var addPropertyName = map.Methods.Single(m => m.Name == "AddPropertyName");
            var addNestedTypeName = map.Methods.Single(m => m.Name == "AddNestedTypeName");

            dict.Clear();
            ILProcessor processor = constructor.Body.GetILProcessor();
            var last = processor.Body.Instructions.Single(i => i.OpCode == OpCodes.Ret);
            foreach (var entry in renameData)
            {
                var member = entry.Key;
                var newName = entry.Value;

                Stack<Instruction> procedure = new Stack<Instruction>();
                if (member.DeclaringType != null)
                {
                    var declaringTypeNewName = GetTypeNewName(renameData, member.DeclaringType, keepNamespaces);
                    switch (member.MetadataToken.TokenType)
                    {
                        case TokenType.Method:
                            if (!options.HasFlag(ReflectionOptions.Methods))
                                continue;
                            var method = (MethodDefinition)member;
                            procedure = AddMemberWithParameters(method, newName, declaringTypeNewName,
                                GetParametersString(method.Parameters, renameData, keepNamespaces), addMethodName);
                            break;
                        case TokenType.Property:
                            if (!options.HasFlag(ReflectionOptions.Properties))
                                continue;
                            var property = (PropertyDefinition)member;
                            procedure = AddMemberWithParameters(property, newName, declaringTypeNewName,
                                GetParametersString(property.Parameters, renameData, keepNamespaces), addPropertyName);
                            break;
                        case TokenType.Field:
                            if (!options.HasFlag(ReflectionOptions.Fields))
                                continue;
                            var field = (FieldDefinition)member;
                            procedure = AddMember(field, newName, declaringTypeNewName, addFieldName);
                            break;
                        case TokenType.Event:
                            if (!options.HasFlag(ReflectionOptions.Events))
                                continue;
                            var @event = (EventDefinition)member;
                            procedure = AddMember(@event, newName, declaringTypeNewName, addEventName);
                            break;
                        case TokenType.TypeDef:
                            if (!options.HasFlag(ReflectionOptions.NestedTypes))
                                continue;
                            var type = (TypeDefinition)member;
                            procedure = AddMember(type, newName, declaringTypeNewName, addNestedTypeName);
                            break;
                    }
                }
                else if (options.HasFlag(ReflectionOptions.Types))
                    procedure = AddTypeName((TypeDefinition)member, newName, addTypeName, keepNamespaces);

                foreach (var instruction in procedure)
                    processor.InsertBefore(last, instruction);
            }
        }
        public static string GetDisplayNameOr(Type type, string memberName, string defaultVal, ReflectionOptions refOptions = ReflectionOptions.Default)
        {
            if (type is null)
            {
                return(string.Empty);
            }
            if (string.IsNullOrWhiteSpace(memberName))
            {
                return(string.Empty);
            }
            var members = type.GetMembers();

            return(GetDisplayNameOrImpl(members.FirstOrDefault(x => x.Name == memberName), defaultVal, refOptions, ReflectionAmbiguousOptions.IgnoreAmbiguous));
        }
 public static string GetDescription <T>(this T x, Expression <Func <T, object> > expression, ReflectionOptions options = ReflectionOptions.Default)
 {
     return(x is null ? string.Empty : TypeReflections.GetDescription(expression, options));
 }
 public static string GetDescriptionOr <T>(this T x, Expression <Func <T, object> > expression, string defaultVal, ReflectionOptions options = ReflectionOptions.Default)
 {
     return(x is null ? defaultVal : TypeReflections.GetDescriptionOr(expression, defaultVal, options));
 }
 public static string GetDescription(ParameterInfo parameter, ReflectionOptions refOptions = ReflectionOptions.Default)
 {
     return(GetDescriptionImpl(parameter, refOptions, ReflectionAmbiguousOptions.IgnoreAmbiguous));
 }
 /// <summary>
 /// To determine whether the given Attribute is undefined.<br />
 /// 判断给定的特性是否未定义。
 /// </summary>
 /// <param name="member"></param>
 /// <param name="attributeType"></param>
 /// <param name="options"></param>
 /// <returns></returns>
 public static bool IsAttributeNotDefined(this MemberInfo member, Type attributeType, ReflectionOptions options = ReflectionOptions.Default)
 {
     return(!TypeReflections.IsAttributeDefined(member, attributeType, options));
 }
        public static string GetDescriptionOrDisplayName <T>(Expression <Func <T, object> > expression, ReflectionOptions options = ReflectionOptions.Default)
        {
            var member = expression.Body as MemberExpression;

            if (member is null &&
                expression.Body.NodeType == ExpressionType.Convert &&
                expression.Body is UnaryExpression unary)
            {
                member = unary.Operand as MemberExpression;
            }

            return(GetDescriptionOrDisplayNameImpl(member?.Member, options, ReflectionAmbiguousOptions.IgnoreAmbiguous));
        }
 public static bool IsDescriptionDefined(ParameterInfo parameter, ReflectionOptions refOptions = ReflectionOptions.Default)
 {
     return(parameter is not null &&
            (IsAttributeDefined <DescriptionAttribute>(parameter, refOptions) || IsAttributeDefined <DisplayAttribute>(parameter, refOptions)));
 }
 private static string GetDisplayNameOrImpl(ParameterInfo parameter, string defaultVal, ReflectionOptions refOptions, ReflectionAmbiguousOptions ambOptions)
 {
     if (parameter is null)
     {
         return(defaultVal);
     }
     if (GetAttribute(parameter, typeof(DisplayNameAttribute), refOptions, ambOptions) is DisplayNameAttribute displayNameAttribute)
     {
         return(displayNameAttribute.DisplayName);
     }
     if (GetAttribute(parameter, typeof(DisplayAttribute), refOptions, ambOptions) is DisplayAttribute displayAttribute)
     {
         return(displayAttribute.Name);
     }
     return(defaultVal);
 }
 public static string GetDescriptionOrDisplayName(MemberInfo member, ReflectionOptions refOptions = ReflectionOptions.Default)
 {
     return(GetDescriptionOrDisplayNameImpl(member, refOptions, ReflectionAmbiguousOptions.IgnoreAmbiguous));
 }
 public static string GetDisplayNameOr(ParameterInfo parameter, string defaultVal, ReflectionOptions refOptions = ReflectionOptions.Default)
 {
     return(GetDisplayNameOrImpl(parameter, defaultVal, refOptions, ReflectionAmbiguousOptions.IgnoreAmbiguous));
 }
Пример #20
0
 public static bool IsAttributeDefined(ParameterInfo parameter, Type attributeType, ReflectionOptions options) =>
 TypeReflections.IsAttributeDefined(parameter, attributeType, options);
 public static string GetDescription <T>(ReflectionOptions refOptions = ReflectionOptions.Default)
 {
     return(GetDescriptionImpl(typeof(T), refOptions, ReflectionAmbiguousOptions.IgnoreAmbiguous));
 }
 /// <summary>
 /// To determine whether the given Attribute is undefined.<br />
 /// 判断给定的特性是否未定义。
 /// </summary>
 /// <typeparam name="TAttribute">要检查的特性类型</typeparam>
 /// <param name="member">要检查的类型成员</param>
 /// <param name="options">反射选项</param>
 /// <returns>是否不存在</returns>
 public static bool IsAttributeNotDefined <TAttribute>(this MemberInfo member, ReflectionOptions options = ReflectionOptions.Default) where TAttribute : Attribute
 {
     return(!TypeReflections.IsAttributeDefined <TAttribute>(member, options));
 }
 private static string GetDescriptionOrImpl(ParameterInfo parameter, string defaultVal, ReflectionOptions refOptions, ReflectionAmbiguousOptions ambOptions)
 {
     return(parameter is not null && IsDescriptionDefined(parameter, refOptions)
         ? GetDescriptionImpl(parameter, refOptions, ambOptions)
         : defaultVal);
 }
 public static bool IsDescriptionDefined(this MemberInfo member, ReflectionOptions options = ReflectionOptions.Default)
 {
     return(TypeReflections.IsDescriptionDefined(member, options));
 }
Пример #25
0
 public static bool IsDescriptionDefined(MemberInfo member, ReflectionOptions refOptions = ReflectionOptions.Default) =>
 TypeReflections.IsDescriptionDefined(member, refOptions);
 public static string GetDescriptionOr(this MemberInfo member, string defaultVal, ReflectionOptions options = ReflectionOptions.Default)
 {
     return(TypeReflections.GetDescriptionOr(member, defaultVal, options));
 }
Пример #27
0
 public static bool IsDescriptionDefined(ParameterInfo parameter, ReflectionOptions refOptions = ReflectionOptions.Default) =>
 TypeReflections.IsDescriptionDefined(parameter, refOptions);
 public static string GetDescriptionOr(MemberInfo member, string defaultVal, ReflectionOptions refOptions = ReflectionOptions.Default)
 {
     return(GetDescriptionOrImpl(member, defaultVal, refOptions, ReflectionAmbiguousOptions.IgnoreAmbiguous));
 }
Пример #29
0
 public static bool IsAttributeDefined <TAttribute>(MemberInfo member, ReflectionOptions options) where TAttribute : Attribute =>
 TypeReflections.IsAttributeDefined <TAttribute>(member, options);
Пример #30
0
 public InjectReflectionMethodProxies(ReflectionOptions reflectionOptions, bool keepNamespaces)
 {
     _reflectionOptions = reflectionOptions;
 }
 public static string GetDisplayNameOr <T>(string defaultVal, ReflectionOptions refOptions = ReflectionOptions.Default)
 {
     return(GetDisplayNameOrImpl(typeof(T), defaultVal, refOptions, ReflectionAmbiguousOptions.IgnoreAmbiguous));
 }