Esempio n. 1
0
        private static string GetXmlDocsPath(dynamic assembly)
        {
            string path;

            try
            {
                if (assembly == null)
                {
                    return(null);
                }

                var assemblyName = assembly.GetName();
                if (string.IsNullOrEmpty(assemblyName.Name))
                {
                    return(null);
                }

                if (Cache.ContainsKey(assemblyName.FullName))
                {
                    return(null);
                }

                if (!string.IsNullOrEmpty(assembly.Location))
                {
                    var assemblyDirectory = DynamicApis.PathGetDirectoryName((string)assembly.Location);
                    path = DynamicApis.PathCombine(assemblyDirectory, (string)assemblyName.Name + ".xml");
                    if (DynamicApis.FileExists(path))
                    {
                        return(path);
                    }
                }

                if (ObjectExtensions.HasProperty(assembly, "CodeBase"))
                {
                    var codeBase = (string)assembly.CodeBase;
                    if (!string.IsNullOrEmpty(codeBase))
                    {
                        path = DynamicApis.PathCombine(DynamicApis.PathGetDirectoryName(codeBase
                                                                                        .Replace("file:///", string.Empty)), assemblyName.Name + ".xml")
                               .Replace("file:\\", string.Empty);

                        if (DynamicApis.FileExists(path))
                        {
                            return(path);
                        }
                    }
                }

                var currentDomain = Type.GetType("System.AppDomain")?.GetRuntimeProperty("CurrentDomain").GetValue(null);
                if (currentDomain?.HasProperty("BaseDirectory") == true)
                {
                    var baseDirectory = currentDomain.TryGetPropertyValue("BaseDirectory", "");
                    if (!string.IsNullOrEmpty(baseDirectory))
                    {
                        path = DynamicApis.PathCombine(baseDirectory, assemblyName.Name + ".xml");
                        if (DynamicApis.FileExists(path))
                        {
                            return(path);
                        }

                        return(DynamicApis.PathCombine(baseDirectory, "bin\\" + assemblyName.Name + ".xml"));
                    }
                }

                var currentDirectory = DynamicApis.DirectoryGetCurrentDirectory();
                path = DynamicApis.PathCombine(currentDirectory, assembly.GetName().Name + ".xml");
                if (DynamicApis.FileExists(path))
                {
                    return(path);
                }

                path = DynamicApis.PathCombine(currentDirectory, "bin\\" + assembly.GetName().Name + ".xml");
                if (DynamicApis.FileExists(path))
                {
                    return(path);
                }

                return(null);
            }
            catch
            {
                return(null);
            }
        }
Esempio n. 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("[[", "{")
                                                         .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("+", ".")));
        }