Пример #1
0
        /// <exception cref="ArgumentException">Unknown member type.</exception>
        internal static string GetMemberElementName(dynamic member)
        {
            char   prefixCode;
            string memberName;
            string memberTypeName;

            var memberType = ((object)member).GetType();

            if (memberType.FullName.Contains(".Cecil."))
            {
                memberName = TypeExtensions.IsAssignableToTypeName(memberType, "TypeDefinition", TypeNameStyle.Name) ?
                             member.FullName : member.DeclaringType.FullName + "." + member.Name;

                memberName = memberName
                             .Replace("/", ".")
                             .Replace('+', '.');

                memberTypeName =
                    TypeExtensions.IsAssignableToTypeName(memberType, "MethodDefinition", TypeNameStyle.Name) ? (memberName.EndsWith("..ctor") ? "Constructor" : "Method") :
                    TypeExtensions.IsAssignableToTypeName(memberType, "PropertyDefinition", TypeNameStyle.Name) ? "Property" :
                    "TypeInfo";
            }
            else
            {
                memberName = member is Type type && !string.IsNullOrEmpty(memberType.FullName) ?
                             type.FullName : member.DeclaringType.FullName + "." + member.Name;

                memberTypeName = (string)member.MemberType.ToString();
            }

            switch (memberTypeName)
            {
            case "Constructor":
                memberName = memberName.Replace(".ctor", "#ctor");
                goto case "Method";

            case "Method":
                prefixCode = 'M';

                Func <dynamic, string> parameterTypeSelector = p => (string)p.ParameterType.FullName;

                var parameters = member is MethodBase ?
                                 ((MethodBase)member).GetParameters().Select(x => x.ParameterType.FullName) :
                                 (IEnumerable <string>)System.Linq.Enumerable.Select <dynamic, string>(member.Parameters, parameterTypeSelector);

                var paramTypesList = string.Join(",", parameters
                                                 .Select(x => Regex
                                                         .Replace(x, "(`[0-9]+)|(, .*?PublicKeyToken=[0-9a-z]*)", string.Empty)
                                                         .Replace("[[", "{")
                                                         .Replace("]]", "}"))
                                                 .ToArray());

                if (!string.IsNullOrEmpty(paramTypesList))
                {
                    memberName += "(" + paramTypesList + ")";
                }

                break;

            case "Event":
                prefixCode = 'E';
                break;

            case "Field":
                prefixCode = 'F';
                break;

            case "NestedType":
                memberName = memberName.Replace('+', '.');
                goto case "TypeInfo";

            case "TypeInfo":
                prefixCode = 'T';
                break;

            case "Property":
                prefixCode = 'P';
                break;

            default:
                throw new ArgumentException("Unknown member type.", "member");
            }

            return(string.Format("{0}:{1}", prefixCode, memberName.Replace("+", ".")));
        }
Пример #2
0
        /// <exception cref="ArgumentException">Unknown member type.</exception>
        internal static string GetMemberElementName(dynamic member)
        {
            char   prefixCode;
            string memberName;
            string memberTypeName;

            if (member is MemberInfo memberInfo &&
                memberInfo.DeclaringType != null &&
                memberInfo.DeclaringType.GetTypeInfo().IsGenericType)
            {
                // Resolve member with generic arguments (Ts instead of actual types)
                if (member is PropertyInfo propertyInfo)
                {
                    member = propertyInfo.DeclaringType.GetRuntimeProperty(propertyInfo.Name);
                }
                else
                {
                    member = ((dynamic)member).Module.ResolveMember(((dynamic)member).MetadataToken);
                }
            }

            var memberType = ((object)member).GetType();

            if (memberType.FullName.Contains(".Cecil."))
            {
                memberName = TypeExtensions.IsAssignableToTypeName(memberType, "TypeDefinition", TypeNameStyle.Name) ?
                             member.FullName : member.DeclaringType.FullName + "." + member.Name;

                memberName = memberName
                             .Replace("/", ".")
                             .Replace('+', '.');

                memberTypeName =
                    TypeExtensions.IsAssignableToTypeName(memberType, "MethodDefinition", TypeNameStyle.Name) ? (memberName.EndsWith("..ctor") ? "Constructor" : "Method") :
                    TypeExtensions.IsAssignableToTypeName(memberType, "PropertyDefinition", TypeNameStyle.Name) ? "Property" :
                    "TypeInfo";
            }
            else
            {
                memberName = member is Type type && !string.IsNullOrEmpty(memberType.FullName) ?
                             type.FullName.Split('[')[0] : ((string)member.DeclaringType.FullName).Split('[')[0] + "." + member.Name;

                memberTypeName = (string)member.MemberType.ToString();
            }

            switch (memberTypeName)
            {
            case "Constructor":
                memberName = memberName.Replace(".ctor", "#ctor");
                goto case "Method";

            case "Method":
                prefixCode = 'M';

                Func <dynamic, string> parameterTypeSelector = p =>
                                                               p.ParameterType.ContainsGenericParameter ?
                                                               (ObjectExtensions.HasProperty(p.ParameterType, "GenericArguments") && p.ParameterType.GenericArguments.Count > 0 ?
                                                                ((string)p.ParameterType.FullName).Split('`')[0] + "{" + string.Join(",", ((ICollection)p.ParameterType.GenericArguments).Cast <dynamic>().Select(u => "||" + u.Position)) + "}" :
                                                                "||" + p.ParameterType.Position) :
                                                               (string)p.ParameterType.FullName;

                var parameters = member is MethodBase ?
                                 ((MethodBase)member).GetParameters().Select(x =>
                                                                             x.ParameterType.FullName ??
                                                                             (((dynamic)x.ParameterType).GenericTypeArguments.Length > 0 ?
                                                                              x.ParameterType.Namespace + "." + x.ParameterType.Name.Split('`')[0] +
                                                                              "{" + string.Join(",", ((Type[])((dynamic)x.ParameterType).GenericTypeArguments).Select(a => "||" + a.GenericParameterPosition.ToString())) + "}" :
                                                                              "||" + x.ParameterType.GenericParameterPosition)) :
                                 (IEnumerable <string>)System.Linq.Enumerable.Select <dynamic, string>(member.Parameters, parameterTypeSelector);

                var paramTypesList = string.Join(",", parameters
                                                 .Select(x => Regex
                                                         .Replace(x, "(`[0-9]+)|(, .*?PublicKeyToken=[0-9a-z]*)", string.Empty)
                                                         .Replace("||", "`")
                                                         .Replace("[[", "{")
                                                         .Replace("]]", "}"))
                                                 .ToArray());

                if (!string.IsNullOrEmpty(paramTypesList))
                {
                    memberName += "(" + paramTypesList + ")";
                }

                break;

            case "Event":
                prefixCode = 'E';
                break;

            case "Field":
                prefixCode = 'F';
                break;

            case "NestedType":
                memberName = memberName.Replace('+', '.');
                goto case "TypeInfo";

            case "TypeInfo":
                prefixCode = 'T';
                break;

            case "Property":
                prefixCode = 'P';
                break;

            default:
                throw new ArgumentException("Unknown member type.", "member");
            }

            return(string.Format("{0}:{1}", prefixCode, memberName.Replace("+", ".")));
        }