Exemple #1
0
        private object ResolveType(AssetIdentifier assetId, Assembly hintAssembly)
        {
            string typeName   = assetId.AssetId.Substring(assetId.TypeMarker.Length + 1);
            int    startIndex = 0;

            return(this.GetDeclaringType(typeName, ref startIndex, hintAssembly));
        }
Exemple #2
0
        private MemberInfo ResolveMethod(AssetIdentifier assetId, Assembly hintAssembly)
        {
            string asset = assetId.AssetId.Substring(2);

            int  startIndex = 0;
            Type type       = this.GetDeclaringType(asset, ref startIndex, hintAssembly);

            const BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance;
            var allMethods =
                type.GetMethods(bindingFlags)
                .Concat <MethodBase>(type.GetConstructors(bindingFlags));

            MethodBase[] methods =
                allMethods.Where(
                    m =>
                    (m is ConstructorInfo &&
                     assetId.AssetId.Equals(Naming.GetAssetId((ConstructorInfo)m),
                                            StringComparison.Ordinal)) ||
                    (m is MethodInfo &&
                     assetId.AssetId.Equals(Naming.GetAssetId((MethodInfo)m), StringComparison.Ordinal)))
                .ToArray();


            // if there is a "new" method on the type we'll find both it and the method it hides.
            if (methods.Length == 2 && methods[1].DeclaringType == type)
            {
                return(methods[1]);
            }

            Debug.Assert(methods.Length == 1 || methods.Length == 2,
                         string.Format("Found {0} methods, expected 1 or 2.", methods.Length));

            return(methods[0]);
        }
Exemple #3
0
        private PropertyInfo ResolveProperty(AssetIdentifier assetId, Assembly hintAssembly)
        {
            string asset = assetId.AssetId.Substring(2);

            int  startIndex = 0;
            Type type       = this.GetDeclaringType(asset, ref startIndex, hintAssembly);

            PropertyInfo[] allProps =
                type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static |
                                   BindingFlags.Instance);
            PropertyInfo[] properties =
                allProps
                .Where(p => assetId.AssetId.Equals(Naming.GetAssetId(p), StringComparison.Ordinal))
                .ToArray();

            // if there is a "new" property on the type we'll find both it and the property it hides.
            if (properties.Length == 2 && properties[1].DeclaringType == type)
            {
                return(properties[1]);
            }

            Debug.Assert(properties.Length == 1 || properties.Length == 2,
                         string.Format("Found {0} properties, expected 1 or 2.", properties.Length));

            return(properties[0]);
        }
Exemple #4
0
        private object ResolveField(AssetIdentifier assetIdentifier, Assembly hintAssembly)
        {
            string asset = assetIdentifier.AssetId.Substring(assetIdentifier.TypeMarker.Length + 1);

            int  startIndex = 0;
            Type type       = this.GetDeclaringType(asset, ref startIndex, hintAssembly);

            if (type.IsEnum)
            {
                MemberInfo[] members =
                    type.GetMembers(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance |
                                    BindingFlags.Static);

                foreach (MemberInfo memberInfo in members)
                {
                    AssetIdentifier ret = AssetIdentifier.FromMemberInfo(memberInfo);
                    if (ret.AssetId == assetIdentifier.AssetId)
                    {
                        return(memberInfo);
                    }
                }

                return(null);
            }
            else
            {
                FieldInfo[] allFields =
                    type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static |
                                   BindingFlags.Instance);

                return(allFields.Single(f => Naming.GetAssetId(f).Equals(assetIdentifier.AssetId)));
            }
        }
Exemple #5
0
        public bool AddReference(string assetId)
        {
            if (string.IsNullOrEmpty(assetId))
            {
                throw new ArgumentException("Argument cannot be null or empty.", "assetId");
            }

            AssetIdentifier aid = AssetIdentifier.Parse(assetId);

            Debug.Assert(aid.ToString().Equals(assetId, StringComparison.Ordinal),
                         "AssetIdentifier '{0}' failed to roundtrip.", assetId);

            if (aid.AssetId[aid.AssetId.Length - 1] == ']')
            {
                aid = new AssetIdentifier(aid.AssetId.Substring(0, aid.AssetId.LastIndexOf('[')),
                                          aid.Version);
            }

            object resolve = this.AssetResolver.Resolve(aid);

            Debug.Assert(resolve != null);

            Debug.Assert(!string.IsNullOrWhiteSpace(aid.AssetId));

            if (this._references.Add(aid))
            {
                TraceSources.GeneratorSource.TraceEvent(TraceEventType.Verbose, 1, "Reference: {0}", aid);
                return(true);
            }

            return(false);
        }
Exemple #6
0
        public bool Filter(IFilterContext context, AssetIdentifier asset)
        {
            Type t = context.AssetResolver.Resolve(asset) as Type;

            if (t != null)
            {
                return(Filter(context, t));
            }

            return(false);
        }
Exemple #7
0
        public bool Filter(IFilterContext context, AssetIdentifier asset)
        {
            MemberInfo mi = context.AssetResolver.Resolve(asset) as MemberInfo;

            if (mi != null)
            {
                return(Filter(context, mi));
            }

            return(false);
        }
Exemple #8
0
        public object Resolve(AssetIdentifier assetIdentifier, AssetIdentifier hintAssembly)
        {
            object ret;

            if (!this._cache.TryGetValue(assetIdentifier, out ret))
            {
                this._cache.Add(assetIdentifier, ret = this.ResolveInternal(assetIdentifier, (Assembly)this.ResolveInternal(hintAssembly)));
            }

            return(ret);
        }
Exemple #9
0
        private EventInfo ResolveEvent(AssetIdentifier assetIdentifier, Assembly hintAssembly)
        {
            string asset = assetIdentifier.AssetId.Substring(assetIdentifier.TypeMarker.Length + 1);

            int  startIndex = 0;
            Type type       = this.GetDeclaringType(asset, ref startIndex, hintAssembly);

            EventInfo[] allEvents =
                type.GetEvents(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static |
                               BindingFlags.Instance);

            return(allEvents.Single(e => Naming.GetAssetId(e).Equals(assetIdentifier.AssetId)));
        }
Exemple #10
0
        private MethodInfo[] ResolveOverloads(AssetIdentifier assetIdentifier, Assembly hintAssembly)
        {
            string asset = assetIdentifier.AssetId.Substring(assetIdentifier.TypeMarker.Length + 1);

            int  startIndex = 0;
            Type type       = this.GetDeclaringType(asset, ref startIndex, hintAssembly);

            string methodName = asset.Substring(startIndex + 1);

            var allMethods = type.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance);

            return(allMethods
                   .Where(m => m.Name.Equals(methodName, StringComparison.Ordinal))
                   .ToArray());
        }
Exemple #11
0
        public bool IsFiltered(AssetIdentifier assetId)
        {
            IFilterContext filterContext = new FilterContext(this.Cache, this.Container, this.AssetResolver, FilterState.Generating);

            for (int i = 0; i < this._filters.Length; i++)
            {
                if (this._filters[i].Filter(filterContext, assetId))
                {
                    TraceSources.GeneratorSource.TraceEvent(TraceEventType.Verbose,
                                                            0,
                                                            "{0} - Filtered by {1}",
                                                            assetId.AssetId, this._filters[i]);
                    return(true);
                }
            }

            return(false);
        }
        public LostDocFileInfo(string path)
        {
            this._assemblies = new List <AssemblyInfo>();
            this.Path        = path;
            XDocument xDoc = XDocument.Load(path);

            foreach (XElement asmElement in xDoc.Root.Elements("assembly"))
            {
                // name="Company.Project.AnotherLibrary" filename="Company.Project.AnotherLibrary.dll" assetId="{A:Company.Project.AnotherLibrary, V:2.1.3.5670}"
                this._assemblies.Add(new AssemblyInfo
                {
                    Name     = asmElement.Attribute("name").Value,
                    Filename = asmElement.Attribute("filename").Value,
                    AssetId  = AssetIdentifier.Parse(asmElement.Attribute("assetId").Value),
                    Phase    = int.Parse(asmElement.Attribute("phase").Value, CultureInfo.InvariantCulture),
                });
            }
        }
Exemple #13
0
        private object ResolveAssembly(AssetIdentifier assetId, Assembly hintAssembly)
        {
            IEnumerable <Assembly> referencedAssemblies =
                this._assemblyLoader.SelectMany(
                    a =>
                    a.GetReferencedAssemblies().Select(
                        n =>
                        Assembly.ReflectionOnlyLoad(n.FullName)));

            IEnumerable <Assembly> assemblies = this._assemblyLoader.Concat(referencedAssemblies);

            foreach (Assembly assembly in assemblies)
            {
                if (AssetIdentifier.FromAssembly(assembly).Equals(assetId))
                {
                    return(assembly);
                }
            }

            return(null);
        }
Exemple #14
0
        private object ResolveInternal(AssetIdentifier assetIdentifier, Assembly hintAssembly = null)
        {
            switch (assetIdentifier.Type)
            {
            case AssetType.Unknown:
                if (StringComparer.Ordinal.Equals("Overload", assetIdentifier.TypeMarker))
                {
                    return(this.ResolveOverloads(assetIdentifier, hintAssembly));
                }
                throw new ArgumentException(string.Format("Type '{0}' not supported.", assetIdentifier.TypeMarker),
                                            "assetIdentifier");

            case AssetType.Namespace:
                return(assetIdentifier.AssetId.Substring(assetIdentifier.TypeMarker.Length + 1));

            case AssetType.Type:
                return(this.ResolveType(assetIdentifier, hintAssembly));

            case AssetType.Method:
                return(this.ResolveMethod(assetIdentifier, hintAssembly));

            case AssetType.Field:
                return(this.ResolveField(assetIdentifier, hintAssembly));

            case AssetType.Event:
                return(this.ResolveEvent(assetIdentifier, hintAssembly));

            case AssetType.Property:
                return(this.ResolveProperty(assetIdentifier, hintAssembly));

            case AssetType.Assembly:
                return(this.ResolveAssembly(assetIdentifier, hintAssembly));

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Exemple #15
0
        public IEnumerable <AssetIdentifier> GetAssetHierarchy(AssetIdentifier assetId)
        {
            if (assetId.Type == AssetType.Unknown)
            {
                yield break;
            }

            yield return(assetId);

            switch (assetId.Type)
            {
            case AssetType.Namespace:
                string     ns = (string)this.Resolve(assetId);
                Assembly[] matchingAssemblies =
                    this._assemblyLoader.Where(a => a.GetName().Version == assetId.Version)
                    .Where(a => a.GetTypes().Any(
                               t1 =>
                               t1.Namespace != null &&
                               (StringComparer.Ordinal.Equals(t1.Namespace, ns) ||
                                t1.Namespace.StartsWith(ns + ".", StringComparison.Ordinal))))
                    .ToArray();

                if (matchingAssemblies.Length == 0)
                {
                    throw new InvalidOperationException("Found no assembly containing namespace: " + ns);
                }

                if (matchingAssemblies.Length > 1)
                {
                    TraceSources.AssetResolverSource.TraceWarning(
                        "Namespace {0} found in more than one assembly: {1}",
                        ns,
                        string.Join(", ", matchingAssemblies.Select(a => a.GetName().Name)));
                }
                yield return(AssetIdentifier.FromAssembly(matchingAssemblies[0]));

                break;

            case AssetType.Type:

                Type t = (Type)this.Resolve(assetId);
                while (t.IsNested)
                {
                    t = t.DeclaringType;
                    yield return(AssetIdentifier.FromMemberInfo(t));
                }

                yield return(AssetIdentifier.FromNamespace(t.Namespace, t.Assembly.GetName().Version));

                yield return(AssetIdentifier.FromAssembly(t.Assembly));

                break;

            case AssetType.Method:
            case AssetType.Field:
            case AssetType.Event:
            case AssetType.Property:
                object     resolve = this.Resolve(assetId);
                MemberInfo mi      = (MemberInfo)resolve;

                foreach (
                    AssetIdentifier aid in
                    this.GetAssetHierarchy(AssetIdentifier.FromMemberInfo(mi.ReflectedType)))
                {
                    yield return(aid);
                }


                break;

            case AssetType.Assembly:
                yield break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Exemple #16
0
        protected internal Type GetDeclaringType(string typeName, ref int startIndex, Assembly hintAssembly)
        {
            Type ret = null;

            string[] fragments = typeName.Substring(startIndex).Split('.');

            for (int i = 0; i < fragments.Length; i++)
            {
                int    fragmentEndsAt = fragments[i].IndexOfAny(new[] { '{', '+' });
                string fragment;

                fragment = fragmentEndsAt == -1 ? fragments[i] : fragments[i].Substring(0, fragmentEndsAt);

                if (ret == null)
                {
                    string possibleTypeName = string.Join(".", fragments, 0, i);

                    if (possibleTypeName.Length <= 0)
                    {
                        possibleTypeName = fragment;
                    }
                    else
                    {
                        possibleTypeName += '.' + fragment;
                    }

                    if (this.TryGetType(possibleTypeName, hintAssembly, out ret))
                    {
                        startIndex += possibleTypeName.Length;
                    }
                    else if (fragmentEndsAt >= 0 &&
                             this.TryGetType(possibleTypeName + fragments[i].Substring(fragmentEndsAt), hintAssembly, out ret))
                    {
                        startIndex    += possibleTypeName.Length + (fragments[i].Length - fragmentEndsAt);
                        fragmentEndsAt = -1;
                    }
                }
                else
                {
                    Type nested = ret.GetNestedType(fragment, BindingFlags.Public | BindingFlags.NonPublic);
                    if (nested == null)
                    {
                        break;
                    }

                    ret = nested;
                    // +1 to account for the seperator that is requried for nested types
                    startIndex += fragment.Length + 1;
                }

                if (fragmentEndsAt != -1 && fragments[i][fragmentEndsAt] == '{')
                {
                    Debug.Assert(ret != null);
                    AssetIdentifier[] typeArgs    = AssetIdentifier.ParseTypeArgs(typeName, ref startIndex).ToArray();
                    Type[]            genTypeArgs = new Type[typeArgs.Length];

                    for (int j = 0; j < typeArgs.Length; j++)
                    {
                        genTypeArgs[j] = (Type)this.ResolveType(typeArgs[j], hintAssembly);
                    }

                    ret = ret.MakeGenericType(genTypeArgs);
                    break;
                }
            }

            return(ret);
        }
Exemple #17
0
        public XDocument Merge(out AssetRedirectCollection assetRedirects)
        {
            // merge assemblies
            Func <AssetIdentifier, string> keySelector;

            assetRedirects = new AssetRedirectCollection();
            if (this._ignoreVersionComponent.HasValue)
            {
                TraceSources.BundleSource.TraceInformation("Merging assembly sections with version mask: {0}",
                                                           this._ignoreVersionComponent.Value.ToString());

                keySelector =
                    aId =>
                    string.Format("{0}, {1}",
                                  aId.AssetId,
                                  aId.Version.ToString((int)this._ignoreVersionComponent.Value));


                IEnumerable <XElement> allAssemblies = this._bundle.Root.XPathSelectElements("assembly[@phase = '0']");

                foreach (XElement asm in allAssemblies)
                {
                    Debug.WriteLine(keySelector(AssetIdentifier.Parse(asm.Attribute("assetId").Value)));
                }

                IEnumerable <IOrderedEnumerable <XElement> > groupedAssemblies =
                    allAssemblies.GroupBy(xe => keySelector(AssetIdentifier.Parse(xe.Attribute("assetId").Value)),
                                          (key, grp) =>
                                          grp.OrderByDescending(
                                              xe =>
                                              AssetIdentifier.Parse(xe.Attribute("assetId").Value).
                                              Version));

                foreach (IOrderedEnumerable <XElement> assemblyGroup in groupedAssemblies)
                {
                    XElement primary            = assemblyGroup.First();
                    IEnumerable <XElement> rest = assemblyGroup.Skip(1);

                    IEnumerable <XAttribute> assetAttrs =
                        ((IEnumerable)primary.XPathEvaluate(".//@assetId")).Cast <XAttribute>();
                    Dictionary <string, AssetIdentifier> assets =
                        assetAttrs.Select(a => AssetIdentifier.Parse(a.Value)).ToDictionary(a => a.AssetId);


                    TraceSources.BundleSource.TraceInformation("Primary assembly: " +
                                                               primary.Attribute("assetId").Value);

                    foreach (XElement secondary in rest)
                    {
                        TraceSources.BundleSource.TraceInformation("Shadowed assembly: " +
                                                                   secondary.Attribute("assetId").Value);
                        secondary.Remove();
                        IEnumerable <XAttribute> secondaryAssetAttrs =
                            ((IEnumerable)secondary.XPathEvaluate(".//@assetId")).Cast <XAttribute>();
                        IEnumerable <AssetIdentifier> secondaryAssets =
                            secondaryAssetAttrs.Select(a => AssetIdentifier.Parse(a.Value));

                        foreach (AssetIdentifier asset in secondaryAssets)
                        {
                            AssetIdentifier primaryAsset;
                            if (assets.TryGetValue(asset.AssetId, out primaryAsset))
                            {
                                assetRedirects.Add(asset, primaryAsset);
                            }
                            else
                            {
                                // warnings, we merged and lost an asset!
                                TraceSources.BundleSource.TraceWarning(
                                    "Failed to redirect asset {0}, no matching asset found in assembly {1}",
                                    asset.ToString(),
                                    primary.Attribute("assetId"));
                            }
                        }
                    }
                }
            }

            TraceSources.BundleSource.TraceInformation("Sorting assemblies by version.");
            XElement[] asmElements =
                this._bundle.Root.Elements().OrderByDescending(
                    e =>
                    AssetIdentifier.Parse(e.Attribute("assetId").Value).
                    Version).ToArray();
            this._bundle.Root.RemoveNodes();
            this._bundle.Root.Add(asmElements);
            return(this._bundle);
        }