XamlBuildTaskServices.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / NetFx40 / XamlBuildTask / Microsoft / Build / Tasks / Xaml / XamlBuildTaskServices.cs / 1407647 / XamlBuildTaskServices.cs

                            //------------------------------------------------------------ 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------

namespace Microsoft.Build.Tasks.Xaml 
{
    using System; 
    using System.Collections.Generic; 
    using System.IO;
    using System.Security; 
    using System.Xaml;
    using System.Xaml.Schema;
    using System.Xml;
    using System.Xml.Linq; 
    using Microsoft.Build.Utilities;
    using System.Reflection; 
    using System.Globalization; 
    using System.Diagnostics.CodeAnalysis;
    using System.Runtime; 
    using System.CodeDom;
    using System.ComponentModel;
    using System.CodeDom.Compiler;
    using System.Linq; 

    internal static class XamlBuildTaskServices 
    { 

        internal const string ClrNamespaceUriNamespacePart = "clr-namespace:"; 
        internal const string ClrNamespaceUriAssemblyPart = "assembly=";
        internal const string XamlExtension = ".xaml";

 
        //internal static XName SchemaTypeName = XamlSchemaTypeResolver.Default.GetTypeReference(typeof(SchemaType)).Name;
        const string UnknownExceptionErrorCode = "XC1000"; 
        // Update this value if any changes are made to it in System.Xaml\KnownStrings.cs 
        const string serializerReferenceNamePrefix = "__ReferenceID";
 
        internal static string SerializerReferenceNamePrefix
        { get { return serializerReferenceNamePrefix; } }

        static string _private = String.Empty; 
        internal static string PrivateModifier
        { get { return _private; } } 
 
        static string _public = String.Empty;
        internal static string PublicModifier 
        { get { return _public; } }

        static string _internal = String.Empty;
        internal static string InternalModifier 
        { get { return _internal; } }
 
        static string _protected = String.Empty; 
        internal static string ProtectedModifier
        { get { return _protected; } } 

        static string _protectedInternal = String.Empty;
        internal static string ProtectedInternalModifier
        { get { return _protectedInternal; } } 

        static string _protectedAndInternal = String.Empty; 
        internal static string ProtectedAndInternalModifier 
        { get { return _protectedAndInternal; } }
 
        static string _publicClass = String.Empty;
        internal static string PublicClassModifier
        { get { return _publicClass; } }
 
        static string _internalClass = String.Empty;
        internal static string InternalClassModifier 
        { get { return _internalClass; } } 

        static string _fileNotLoaded = String.Empty; 
        internal static string FileNotLoaded
        {
            get { return _fileNotLoaded; }
        } 

        internal static void PopulateModifiers(CodeDomProvider codeDomProvider) 
        { 
            TypeConverter memberAttributesConverter = codeDomProvider.GetConverter(typeof(MemberAttributes));
            if (memberAttributesConverter != null) 
            {
                if (memberAttributesConverter.CanConvertTo(typeof(string)))
                {
                    try 
                    {
                        _private = memberAttributesConverter.ConvertToInvariantString(MemberAttributes.Private).ToUpperInvariant(); 
                        _public = memberAttributesConverter.ConvertToInvariantString(MemberAttributes.Public).ToUpperInvariant(); 
                        _protected = memberAttributesConverter.ConvertToInvariantString(MemberAttributes.Family).ToUpperInvariant();
                        _internal = memberAttributesConverter.ConvertToInvariantString(MemberAttributes.Assembly).ToUpperInvariant(); 
                        _protectedInternal = memberAttributesConverter.ConvertToInvariantString(MemberAttributes.FamilyOrAssembly).ToUpperInvariant();
                        _protectedAndInternal = memberAttributesConverter.ConvertToInvariantString(MemberAttributes.FamilyAndAssembly).ToUpperInvariant();
                    }
                    catch (NotSupportedException) 
                    {
                    } 
                } 
            }
 
            TypeConverter typeAttributesConverter = codeDomProvider.GetConverter(typeof(TypeAttributes));
            if (typeAttributesConverter != null)
            {
                if (typeAttributesConverter.CanConvertTo(typeof(string))) 
                {
                    try 
                    { 
                        _internalClass = typeAttributesConverter.ConvertToInvariantString(TypeAttributes.NotPublic).ToUpperInvariant();
                        _publicClass = typeAttributesConverter.ConvertToInvariantString(TypeAttributes.Public).ToUpperInvariant(); 
                    }
                    catch (NotSupportedException)
                    {
                    } 
                }
            } 
        } 

        public static Assembly ReflectionOnlyAssemblyResolve(object sender, ResolveEventArgs args) 
        {
            string[] parts = args.Name.Split(',');
            foreach (var asm in AppDomain.CurrentDomain.ReflectionOnlyGetAssemblies())
            { 
                AssemblyName assemblyName = asm.GetName();
 
                if (assemblyName.Name.Equals(parts[0], StringComparison.OrdinalIgnoreCase)) 
                {
                    return asm; 
                }
            }

            // The fact that the file can not be found in the referenced assembly list means that 
            // CLR will throw a FileLoadException once this event handler returns.
            // Since the FileLoadException's FileName property is set to null (due to the fact there is no path), 
            // we are storing it in a static variable so that we can print a pretty error message later on. 

            _fileNotLoaded = args.Name; 

            return null;
        }
 
        public static AppDomain CreateAppDomain(string friendlyName, string buildTaskPath)
        { 
            if (buildTaskPath == null) 
            {
                throw FxTrace.Exception.AsError(new LoggedException(new InvalidOperationException(SR.BuildTaskPathMustNotBeNull))); 
            }

            // Enable shadow copying in the Designer appdomain, so that we can continue to rebuild
            // projects in the solution even if designer has loaded the assemblies that were referenced in current project 
            AppDomainSetup appDomainSetup = new AppDomainSetup();
            appDomainSetup.ShadowCopyFiles = "true"; 
            appDomainSetup.ApplicationBase = buildTaskPath; 
            appDomainSetup.LoaderOptimization = LoaderOptimization.MultiDomainHost;
 
            // Create appdomain with fulltrust.
            return AppDomain.CreateDomain(
                friendlyName,
                AppDomain.CurrentDomain.Evidence, 
                appDomainSetup,
                new NamedPermissionSet("FullTrust")); 
        } 

        internal static IList Load(IList references) 
        {
            IList assemblies = new List();
            foreach (var item in references)
            { 
                assemblies.Add(Load(item));
            } 
 
            bool mscorlibFound = false;
 
            foreach (Assembly asm in assemblies)
            {
                // here we want to check if the assembly is mscorlib.dll.
                // for the current codebase, this check would have worked: 
                // if (asm == typeof(Object).Assembly), but we
                // prefer a check that will continue to work when LMR is used 
                if (asm.GetReferencedAssemblies().Length == 0) 
                {
                    mscorlibFound = true; 
                }
            }

            if (!mscorlibFound) 
            {
                assemblies.Add(typeof(Object).Assembly); 
            } 

            return assemblies; 
        }

        [SuppressMessage(FxCop.Category.Reliability, FxCop.Rule.AvoidCallingProblematicMethods,
            Justification = "Using LoadFile to avoid loading through Fusion and load the exact assembly the developer specified")] 
        internal static Assembly Load(string reference)
        { 
            if (reference.EndsWith("mscorlib.dll", StringComparison.OrdinalIgnoreCase)) 
            {
                return typeof(Object).Assembly; 
            }
            string fullPath = Path.GetFullPath(reference);
            try
            { 
                return Assembly.ReflectionOnlyLoadFrom(fullPath);
            } 
            catch (FileNotFoundException e) 
            {
                if (e.FileName == null) 
                {
                    throw FxTrace.Exception.AsError((new FileNotFoundException(e.Message, fullPath)));
                }
                else 
                    throw;
            } 
        } 

        internal static void LogException(Task task, string message) 
        {
            LogException(task, message, null, 0, 0);
        }
 
        internal static void LogException(Task task, string message, string fileName, int lineNumber, int linePosition)
        { 
            string errorCode, logMessage; 
            ExtractErrorCodeAndMessage(task, message, out errorCode, out logMessage);
            task.Log.LogError(null, errorCode, null, fileName, lineNumber, linePosition, 0, 0, logMessage, null); 
        }

        static void ExtractErrorCodeAndMessage(Task task, string message, out string errorCode, out string logMessage)
        { 
            errorCode = task.Log.ExtractMessageCode(message, out logMessage);
            if (string.IsNullOrEmpty(errorCode)) 
            { 
                errorCode = UnknownExceptionErrorCode;
                logMessage = SR.UnknownBuildError(message); 
            }
        }

        internal static bool IsClrNamespaceUri(string nsName, out int nsIndex, out int assemblyIndex) 
        {
            if (nsName.StartsWith(ClrNamespaceUriNamespacePart, StringComparison.Ordinal)) 
            { 
                int semicolonIndex = nsName.IndexOf(';');
                if (semicolonIndex == -1 || nsName.Trim().EndsWith(";", StringComparison.Ordinal)) 
                {
                    nsIndex = ClrNamespaceUriNamespacePart.Length;
                    assemblyIndex = -1;
                    return true; 
                }
                else 
                { 
                    int equalsIndex = nsName.IndexOf('=', semicolonIndex);
                    if (equalsIndex != -1) 
                    {
                        int start = ClrNamespaceUriNamespacePart.Length;
                        int assemblyStart = semicolonIndex + 1;
                        if (equalsIndex - semicolonIndex == ClrNamespaceUriAssemblyPart.Length) 
                        {
                            for (int i = 0; i < ClrNamespaceUriAssemblyPart.Length; i++) 
                            { 
                                if (nsName[assemblyStart + i] != ClrNamespaceUriAssemblyPart[i])
                                { 
                                    nsIndex = -1;
                                    assemblyIndex = -1;
                                    return false;
                                } 
                            }
 
                            nsIndex = start; 
                            assemblyIndex = assemblyStart + ClrNamespaceUriAssemblyPart.Length;
                            return true; 
                        }
                    }
                }
            } 
            nsIndex = -1;
            assemblyIndex = -1; 
            return false; 
        }
 
        internal static string UpdateClrNamespaceUriWithLocalAssembly(string @namespace, string localAssemblyName)
        {
            return UpdateClrNamespaceUriWithLocalAssembly(@namespace, localAssemblyName, null);
        } 

        internal static string UpdateClrNamespaceUriWithLocalAssembly(string @namespace, string localAssemblyName, string realAssemblyName) 
        { 
            int nsIndex, assemblyIndex;
            if (IsClrNamespaceUri(@namespace, out nsIndex, out assemblyIndex)) 
            {
                // If assembly portion of namespace does not exist, assume that this is part of the local assembly
                if (assemblyIndex == -1)
                { 
                    return string.Format(CultureInfo.InvariantCulture, "{0};{1}{2}",
                        @namespace.TrimEnd(' ', ';'), XamlBuildTaskServices.ClrNamespaceUriAssemblyPart, localAssemblyName); 
                } 
                else if (!string.IsNullOrEmpty(realAssemblyName) &&
                    localAssemblyName != realAssemblyName && 
                    @namespace.Substring(assemblyIndex) == realAssemblyName)
                {
                    return string.Format(CultureInfo.InvariantCulture, "{0}{1}",
                        @namespace.Substring(0, assemblyIndex), localAssemblyName); 
                }
            } 
            return @namespace; 
        }
 
        internal static string GetFullTypeName(XamlType xamlType)
        {
            string typeName = GetFullTypeNameWithoutNamespace(xamlType);
            typeName = xamlType.PreferredXamlNamespace + ":" + typeName; 
            return typeName;
        } 
 
        private static string GetFullTypeNameWithoutNamespace(XamlType xamlType)
        { 
            string typeName = string.Empty;
            if (xamlType != null)
            {
                typeName = xamlType.Name; 
                bool firstTypeArg = true;
                if (xamlType.TypeArguments != null && xamlType.TypeArguments.Count > 0) 
                { 
                    typeName += "(";
                    foreach (XamlType typeArg in xamlType.TypeArguments) 
                    {
                        if (!firstTypeArg)
                        {
                            typeName += ","; 
                        }
                        else 
                        { 
                            firstTypeArg = false;
                        } 
                        typeName += typeArg.Name;
                    }
                    typeName += ")";
                } 
            }
            return typeName; 
        } 

        internal static XamlType GetXamlTypeFromString(string typeName, NamespaceTable namespaceTable, XamlSchemaContext xsc) 
        {
            XamlTypeName xamlTypeName = XamlTypeName.Parse(typeName, namespaceTable);
            XamlType xamlType = xsc.GetXamlType(xamlTypeName);
            if (xamlType == null) 
            {
                xamlType = GetXamlTypeFromXamlTypeName(xamlTypeName, namespaceTable, xsc); 
            } 
            return xamlType;
        } 

        static XamlType GetXamlTypeFromXamlTypeName(XamlTypeName xamlTypeName, NamespaceTable namespaceTable, XamlSchemaContext xsc)
        {
            IList typeArgs = null; 
            if (xamlTypeName.TypeArguments.Count > 0)
            { 
                typeArgs = new List(); 
                foreach (var typeArg in xamlTypeName.TypeArguments)
                { 
                    typeArgs.Add(GetXamlTypeFromXamlTypeName(typeArg, namespaceTable, xsc));
                }
            }
            return new XamlType(xamlTypeName.Namespace, xamlTypeName.Name, typeArgs, xsc); 
        }
 
        internal static bool TryGetClrTypeName(XamlType xamlType, string rootNamespace, out string clrTypeName) 
        {
            bool isLocal; 
            return TryGetClrTypeName(xamlType, rootNamespace, out clrTypeName, out isLocal);
        }

        internal static bool TryGetClrTypeName(XamlType xamlType, string rootNamespace, out string clrTypeName, out bool isLocal) 
        {
            if (!xamlType.IsUnknown) 
            { 
                isLocal = false;
                clrTypeName = xamlType.UnderlyingType != null ? xamlType.UnderlyingType.FullName : null; 
                return (clrTypeName != null);
            }
            else
            { 
                isLocal = true;
                return TryGetClrTypeNameFromLocalType(xamlType, rootNamespace, out clrTypeName); 
            } 
        }
 
        internal static bool TryExtractClrNs(string @namespace, out string clrNs)
        {
            int nsIndex, assemblyIndex;
            if (XamlBuildTaskServices.IsClrNamespaceUri(@namespace, out nsIndex, out assemblyIndex)) 
            {
                clrNs = (assemblyIndex == -1) 
                    ? @namespace.Substring(nsIndex).TrimEnd(' ', ';') 
                    : @namespace.Substring(
                        nsIndex, 
                        assemblyIndex - XamlBuildTaskServices.ClrNamespaceUriAssemblyPart.Length - nsIndex - 1).TrimEnd(' ', ';');
                return true;
            }
            else 
            {
                clrNs = null; 
                return false; 
            }
        } 

        static bool TryGetClrTypeNameFromLocalType(XamlType xamlType, string rootNamespace, out string clrTypeName)
        {
            // 
            // This means that either we have a type in the base hierarchy or type arguments
            // that is invalid or it's in the local (current) project. 
            string @namespace = xamlType.PreferredXamlNamespace; 
            string name = xamlType.Name;
            string clrNs; 

            if (@namespace != null && TryExtractClrNs(@namespace, out clrNs))
            {
                if (!String.IsNullOrEmpty(rootNamespace) && 
                    !String.IsNullOrEmpty(clrNs) &&
                    clrNs.StartsWith(rootNamespace, StringComparison.OrdinalIgnoreCase)) 
                { 
                    if (clrNs.Length > rootNamespace.Length)
                    { 
                        clrNs = clrNs.Substring(rootNamespace.Length + 1);
                    }
                    else
                    { 
                        clrNs = string.Empty;
                    } 
                } 
                if (string.IsNullOrEmpty(clrNs))
                { 
                    clrTypeName = name;
                }
                else
                { 
                    clrTypeName = string.Format(CultureInfo.InvariantCulture, "{0}.{1}", clrNs, name);
                } 
                return true; 
            }
            else 
            {
                if (xamlType.TypeArguments != null && xamlType.TypeArguments.Count > 0)
                {
                    if (GetClrTypeNameFromReflection(xamlType, xamlType.TypeArguments.Count, out clrTypeName)) 
                        return true;
                } 
            } 
            clrTypeName = null;
            return false; 
        }

        static bool GetClrTypeNameFromReflection(XamlType typeName, int argumentCount, out string clrTypeName)
        { 
            clrTypeName = null;
            string @namespace = typeName.PreferredXamlNamespace; 
            string xmlnsDefAttributeName = typeof(System.Windows.Markup.XmlnsDefinitionAttribute).FullName; 
            string xmlnsDefAttributeAssem = typeof(System.Windows.Markup.XmlnsDefinitionAttribute).Assembly.GetName().Name;
 
            var matchingAssemblies =
                //
                // For each reflection-only assembly in the current application domain...
                // 
                from assembly in AppDomain.CurrentDomain.ReflectionOnlyGetAssemblies()
                // 
                // ...go through all the XmlnsDefinitionAttributes in that assembly... 
                //
                from cad in assembly.GetCustomAttributesData() 
                where cad.Constructor.DeclaringType.FullName == xmlnsDefAttributeName &&
                      cad.Constructor.DeclaringType.Assembly.GetName().Name == xmlnsDefAttributeAssem
                //
                // ... that match the specified namespace... 
                //
                where (@namespace == (string)cad.ConstructorArguments[0].Value) 
                // 
                // ... and return an {assembly, clr namespace} pair.
                // 
                select new
                {
                    Assembly = assembly,
                    Namespace = ((string)cad.ConstructorArguments[1].Value) + "." 
                };
 
            foreach (var match in matchingAssemblies) 
            {
                string argumentQualifiedName = argumentCount > 0 ? typeName.Name + "`" + argumentCount : typeName.Name; 
                Type type = match.Assembly.GetType(match.Namespace + argumentQualifiedName, false);

                if (type != null)
                { 
                    clrTypeName = type.FullName;
                    return true; 
                } 
            }
            return false; 
        }

        // Returns true if type namespace is clr-namespace and populates assemblyName
        // Else returns false with assemblyName null; 
        internal static bool GetTypeNameInAssemblyOrNamespace(XamlType type, string localAssemblyName, string realAssemblyName,
            out string typeName, out string assemblyName, out string ns) 
        { 
            int assemblyIndex, nsIndex;
            string typeNs = type.PreferredXamlNamespace; 
            typeName = GetFullTypeNameWithoutNamespace(type);
            if (IsClrNamespaceUri(typeNs, out nsIndex, out assemblyIndex))
            {
                assemblyName = assemblyIndex > -1 ? typeNs.Substring(assemblyIndex) : String.Empty; 
                if ((!string.IsNullOrEmpty(localAssemblyName) && assemblyName.Contains(localAssemblyName)) || string.IsNullOrEmpty(assemblyName))
                { 
                    assemblyName = realAssemblyName; 
                }
                int nsLength = typeNs.IndexOf(';') - nsIndex; 
                if (nsLength > 0)
                {
                    ns = typeNs.Substring(nsIndex, nsLength);
                } 
                else
                { 
                    ns = typeNs.Substring(nsIndex); 
                }
                return true; 
            }
            else
            {
                assemblyName = null; 
                ns = typeNs;
                return false; 
            } 
        }
 
        internal static string GetTypeName(XamlType type, string localAssemblyName, string realAssemblyName)
        {
            string typeName, assemblyName, ns;
            if (GetTypeNameInAssemblyOrNamespace(type, localAssemblyName, realAssemblyName, out typeName, out assemblyName, out ns)) 
            {
                return ns + "." + typeName; 
            } 
            else
            { 
                return ns + ":" + typeName;
            }
        }
 
        // Returns false if the type is known else returns true.
        internal static bool GetUnresolvedLeafTypeArg(XamlType type, ref IList unresolvedLeafTypeList) 
        { 
            if (unresolvedLeafTypeList != null && type != null && type.IsUnknown)
            { 
                if (!type.IsGeneric)
                {
                    unresolvedLeafTypeList.Add(type);
                } 
                else
                { 
                    bool hasUnknownChildren = false; 
                    foreach (XamlType typeArg in type.TypeArguments)
                    { 
                        hasUnknownChildren |= GetUnresolvedLeafTypeArg(typeArg, ref unresolvedLeafTypeList);
                    }
                    if (!hasUnknownChildren)
                    { 
                        unresolvedLeafTypeList.Add(type);
                    } 
                } 
                return true;
            } 
            else
            {
                return false;
            } 
        }
    } 
} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//------------------------------------------------------------ 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------

namespace Microsoft.Build.Tasks.Xaml 
{
    using System; 
    using System.Collections.Generic; 
    using System.IO;
    using System.Security; 
    using System.Xaml;
    using System.Xaml.Schema;
    using System.Xml;
    using System.Xml.Linq; 
    using Microsoft.Build.Utilities;
    using System.Reflection; 
    using System.Globalization; 
    using System.Diagnostics.CodeAnalysis;
    using System.Runtime; 
    using System.CodeDom;
    using System.ComponentModel;
    using System.CodeDom.Compiler;
    using System.Linq; 

    internal static class XamlBuildTaskServices 
    { 

        internal const string ClrNamespaceUriNamespacePart = "clr-namespace:"; 
        internal const string ClrNamespaceUriAssemblyPart = "assembly=";
        internal const string XamlExtension = ".xaml";

 
        //internal static XName SchemaTypeName = XamlSchemaTypeResolver.Default.GetTypeReference(typeof(SchemaType)).Name;
        const string UnknownExceptionErrorCode = "XC1000"; 
        // Update this value if any changes are made to it in System.Xaml\KnownStrings.cs 
        const string serializerReferenceNamePrefix = "__ReferenceID";
 
        internal static string SerializerReferenceNamePrefix
        { get { return serializerReferenceNamePrefix; } }

        static string _private = String.Empty; 
        internal static string PrivateModifier
        { get { return _private; } } 
 
        static string _public = String.Empty;
        internal static string PublicModifier 
        { get { return _public; } }

        static string _internal = String.Empty;
        internal static string InternalModifier 
        { get { return _internal; } }
 
        static string _protected = String.Empty; 
        internal static string ProtectedModifier
        { get { return _protected; } } 

        static string _protectedInternal = String.Empty;
        internal static string ProtectedInternalModifier
        { get { return _protectedInternal; } } 

        static string _protectedAndInternal = String.Empty; 
        internal static string ProtectedAndInternalModifier 
        { get { return _protectedAndInternal; } }
 
        static string _publicClass = String.Empty;
        internal static string PublicClassModifier
        { get { return _publicClass; } }
 
        static string _internalClass = String.Empty;
        internal static string InternalClassModifier 
        { get { return _internalClass; } } 

        static string _fileNotLoaded = String.Empty; 
        internal static string FileNotLoaded
        {
            get { return _fileNotLoaded; }
        } 

        internal static void PopulateModifiers(CodeDomProvider codeDomProvider) 
        { 
            TypeConverter memberAttributesConverter = codeDomProvider.GetConverter(typeof(MemberAttributes));
            if (memberAttributesConverter != null) 
            {
                if (memberAttributesConverter.CanConvertTo(typeof(string)))
                {
                    try 
                    {
                        _private = memberAttributesConverter.ConvertToInvariantString(MemberAttributes.Private).ToUpperInvariant(); 
                        _public = memberAttributesConverter.ConvertToInvariantString(MemberAttributes.Public).ToUpperInvariant(); 
                        _protected = memberAttributesConverter.ConvertToInvariantString(MemberAttributes.Family).ToUpperInvariant();
                        _internal = memberAttributesConverter.ConvertToInvariantString(MemberAttributes.Assembly).ToUpperInvariant(); 
                        _protectedInternal = memberAttributesConverter.ConvertToInvariantString(MemberAttributes.FamilyOrAssembly).ToUpperInvariant();
                        _protectedAndInternal = memberAttributesConverter.ConvertToInvariantString(MemberAttributes.FamilyAndAssembly).ToUpperInvariant();
                    }
                    catch (NotSupportedException) 
                    {
                    } 
                } 
            }
 
            TypeConverter typeAttributesConverter = codeDomProvider.GetConverter(typeof(TypeAttributes));
            if (typeAttributesConverter != null)
            {
                if (typeAttributesConverter.CanConvertTo(typeof(string))) 
                {
                    try 
                    { 
                        _internalClass = typeAttributesConverter.ConvertToInvariantString(TypeAttributes.NotPublic).ToUpperInvariant();
                        _publicClass = typeAttributesConverter.ConvertToInvariantString(TypeAttributes.Public).ToUpperInvariant(); 
                    }
                    catch (NotSupportedException)
                    {
                    } 
                }
            } 
        } 

        public static Assembly ReflectionOnlyAssemblyResolve(object sender, ResolveEventArgs args) 
        {
            string[] parts = args.Name.Split(',');
            foreach (var asm in AppDomain.CurrentDomain.ReflectionOnlyGetAssemblies())
            { 
                AssemblyName assemblyName = asm.GetName();
 
                if (assemblyName.Name.Equals(parts[0], StringComparison.OrdinalIgnoreCase)) 
                {
                    return asm; 
                }
            }

            // The fact that the file can not be found in the referenced assembly list means that 
            // CLR will throw a FileLoadException once this event handler returns.
            // Since the FileLoadException's FileName property is set to null (due to the fact there is no path), 
            // we are storing it in a static variable so that we can print a pretty error message later on. 

            _fileNotLoaded = args.Name; 

            return null;
        }
 
        public static AppDomain CreateAppDomain(string friendlyName, string buildTaskPath)
        { 
            if (buildTaskPath == null) 
            {
                throw FxTrace.Exception.AsError(new LoggedException(new InvalidOperationException(SR.BuildTaskPathMustNotBeNull))); 
            }

            // Enable shadow copying in the Designer appdomain, so that we can continue to rebuild
            // projects in the solution even if designer has loaded the assemblies that were referenced in current project 
            AppDomainSetup appDomainSetup = new AppDomainSetup();
            appDomainSetup.ShadowCopyFiles = "true"; 
            appDomainSetup.ApplicationBase = buildTaskPath; 
            appDomainSetup.LoaderOptimization = LoaderOptimization.MultiDomainHost;
 
            // Create appdomain with fulltrust.
            return AppDomain.CreateDomain(
                friendlyName,
                AppDomain.CurrentDomain.Evidence, 
                appDomainSetup,
                new NamedPermissionSet("FullTrust")); 
        } 

        internal static IList Load(IList references) 
        {
            IList assemblies = new List();
            foreach (var item in references)
            { 
                assemblies.Add(Load(item));
            } 
 
            bool mscorlibFound = false;
 
            foreach (Assembly asm in assemblies)
            {
                // here we want to check if the assembly is mscorlib.dll.
                // for the current codebase, this check would have worked: 
                // if (asm == typeof(Object).Assembly), but we
                // prefer a check that will continue to work when LMR is used 
                if (asm.GetReferencedAssemblies().Length == 0) 
                {
                    mscorlibFound = true; 
                }
            }

            if (!mscorlibFound) 
            {
                assemblies.Add(typeof(Object).Assembly); 
            } 

            return assemblies; 
        }

        [SuppressMessage(FxCop.Category.Reliability, FxCop.Rule.AvoidCallingProblematicMethods,
            Justification = "Using LoadFile to avoid loading through Fusion and load the exact assembly the developer specified")] 
        internal static Assembly Load(string reference)
        { 
            if (reference.EndsWith("mscorlib.dll", StringComparison.OrdinalIgnoreCase)) 
            {
                return typeof(Object).Assembly; 
            }
            string fullPath = Path.GetFullPath(reference);
            try
            { 
                return Assembly.ReflectionOnlyLoadFrom(fullPath);
            } 
            catch (FileNotFoundException e) 
            {
                if (e.FileName == null) 
                {
                    throw FxTrace.Exception.AsError((new FileNotFoundException(e.Message, fullPath)));
                }
                else 
                    throw;
            } 
        } 

        internal static void LogException(Task task, string message) 
        {
            LogException(task, message, null, 0, 0);
        }
 
        internal static void LogException(Task task, string message, string fileName, int lineNumber, int linePosition)
        { 
            string errorCode, logMessage; 
            ExtractErrorCodeAndMessage(task, message, out errorCode, out logMessage);
            task.Log.LogError(null, errorCode, null, fileName, lineNumber, linePosition, 0, 0, logMessage, null); 
        }

        static void ExtractErrorCodeAndMessage(Task task, string message, out string errorCode, out string logMessage)
        { 
            errorCode = task.Log.ExtractMessageCode(message, out logMessage);
            if (string.IsNullOrEmpty(errorCode)) 
            { 
                errorCode = UnknownExceptionErrorCode;
                logMessage = SR.UnknownBuildError(message); 
            }
        }

        internal static bool IsClrNamespaceUri(string nsName, out int nsIndex, out int assemblyIndex) 
        {
            if (nsName.StartsWith(ClrNamespaceUriNamespacePart, StringComparison.Ordinal)) 
            { 
                int semicolonIndex = nsName.IndexOf(';');
                if (semicolonIndex == -1 || nsName.Trim().EndsWith(";", StringComparison.Ordinal)) 
                {
                    nsIndex = ClrNamespaceUriNamespacePart.Length;
                    assemblyIndex = -1;
                    return true; 
                }
                else 
                { 
                    int equalsIndex = nsName.IndexOf('=', semicolonIndex);
                    if (equalsIndex != -1) 
                    {
                        int start = ClrNamespaceUriNamespacePart.Length;
                        int assemblyStart = semicolonIndex + 1;
                        if (equalsIndex - semicolonIndex == ClrNamespaceUriAssemblyPart.Length) 
                        {
                            for (int i = 0; i < ClrNamespaceUriAssemblyPart.Length; i++) 
                            { 
                                if (nsName[assemblyStart + i] != ClrNamespaceUriAssemblyPart[i])
                                { 
                                    nsIndex = -1;
                                    assemblyIndex = -1;
                                    return false;
                                } 
                            }
 
                            nsIndex = start; 
                            assemblyIndex = assemblyStart + ClrNamespaceUriAssemblyPart.Length;
                            return true; 
                        }
                    }
                }
            } 
            nsIndex = -1;
            assemblyIndex = -1; 
            return false; 
        }
 
        internal static string UpdateClrNamespaceUriWithLocalAssembly(string @namespace, string localAssemblyName)
        {
            return UpdateClrNamespaceUriWithLocalAssembly(@namespace, localAssemblyName, null);
        } 

        internal static string UpdateClrNamespaceUriWithLocalAssembly(string @namespace, string localAssemblyName, string realAssemblyName) 
        { 
            int nsIndex, assemblyIndex;
            if (IsClrNamespaceUri(@namespace, out nsIndex, out assemblyIndex)) 
            {
                // If assembly portion of namespace does not exist, assume that this is part of the local assembly
                if (assemblyIndex == -1)
                { 
                    return string.Format(CultureInfo.InvariantCulture, "{0};{1}{2}",
                        @namespace.TrimEnd(' ', ';'), XamlBuildTaskServices.ClrNamespaceUriAssemblyPart, localAssemblyName); 
                } 
                else if (!string.IsNullOrEmpty(realAssemblyName) &&
                    localAssemblyName != realAssemblyName && 
                    @namespace.Substring(assemblyIndex) == realAssemblyName)
                {
                    return string.Format(CultureInfo.InvariantCulture, "{0}{1}",
                        @namespace.Substring(0, assemblyIndex), localAssemblyName); 
                }
            } 
            return @namespace; 
        }
 
        internal static string GetFullTypeName(XamlType xamlType)
        {
            string typeName = GetFullTypeNameWithoutNamespace(xamlType);
            typeName = xamlType.PreferredXamlNamespace + ":" + typeName; 
            return typeName;
        } 
 
        private static string GetFullTypeNameWithoutNamespace(XamlType xamlType)
        { 
            string typeName = string.Empty;
            if (xamlType != null)
            {
                typeName = xamlType.Name; 
                bool firstTypeArg = true;
                if (xamlType.TypeArguments != null && xamlType.TypeArguments.Count > 0) 
                { 
                    typeName += "(";
                    foreach (XamlType typeArg in xamlType.TypeArguments) 
                    {
                        if (!firstTypeArg)
                        {
                            typeName += ","; 
                        }
                        else 
                        { 
                            firstTypeArg = false;
                        } 
                        typeName += typeArg.Name;
                    }
                    typeName += ")";
                } 
            }
            return typeName; 
        } 

        internal static XamlType GetXamlTypeFromString(string typeName, NamespaceTable namespaceTable, XamlSchemaContext xsc) 
        {
            XamlTypeName xamlTypeName = XamlTypeName.Parse(typeName, namespaceTable);
            XamlType xamlType = xsc.GetXamlType(xamlTypeName);
            if (xamlType == null) 
            {
                xamlType = GetXamlTypeFromXamlTypeName(xamlTypeName, namespaceTable, xsc); 
            } 
            return xamlType;
        } 

        static XamlType GetXamlTypeFromXamlTypeName(XamlTypeName xamlTypeName, NamespaceTable namespaceTable, XamlSchemaContext xsc)
        {
            IList typeArgs = null; 
            if (xamlTypeName.TypeArguments.Count > 0)
            { 
                typeArgs = new List(); 
                foreach (var typeArg in xamlTypeName.TypeArguments)
                { 
                    typeArgs.Add(GetXamlTypeFromXamlTypeName(typeArg, namespaceTable, xsc));
                }
            }
            return new XamlType(xamlTypeName.Namespace, xamlTypeName.Name, typeArgs, xsc); 
        }
 
        internal static bool TryGetClrTypeName(XamlType xamlType, string rootNamespace, out string clrTypeName) 
        {
            bool isLocal; 
            return TryGetClrTypeName(xamlType, rootNamespace, out clrTypeName, out isLocal);
        }

        internal static bool TryGetClrTypeName(XamlType xamlType, string rootNamespace, out string clrTypeName, out bool isLocal) 
        {
            if (!xamlType.IsUnknown) 
            { 
                isLocal = false;
                clrTypeName = xamlType.UnderlyingType != null ? xamlType.UnderlyingType.FullName : null; 
                return (clrTypeName != null);
            }
            else
            { 
                isLocal = true;
                return TryGetClrTypeNameFromLocalType(xamlType, rootNamespace, out clrTypeName); 
            } 
        }
 
        internal static bool TryExtractClrNs(string @namespace, out string clrNs)
        {
            int nsIndex, assemblyIndex;
            if (XamlBuildTaskServices.IsClrNamespaceUri(@namespace, out nsIndex, out assemblyIndex)) 
            {
                clrNs = (assemblyIndex == -1) 
                    ? @namespace.Substring(nsIndex).TrimEnd(' ', ';') 
                    : @namespace.Substring(
                        nsIndex, 
                        assemblyIndex - XamlBuildTaskServices.ClrNamespaceUriAssemblyPart.Length - nsIndex - 1).TrimEnd(' ', ';');
                return true;
            }
            else 
            {
                clrNs = null; 
                return false; 
            }
        } 

        static bool TryGetClrTypeNameFromLocalType(XamlType xamlType, string rootNamespace, out string clrTypeName)
        {
            // 
            // This means that either we have a type in the base hierarchy or type arguments
            // that is invalid or it's in the local (current) project. 
            string @namespace = xamlType.PreferredXamlNamespace; 
            string name = xamlType.Name;
            string clrNs; 

            if (@namespace != null && TryExtractClrNs(@namespace, out clrNs))
            {
                if (!String.IsNullOrEmpty(rootNamespace) && 
                    !String.IsNullOrEmpty(clrNs) &&
                    clrNs.StartsWith(rootNamespace, StringComparison.OrdinalIgnoreCase)) 
                { 
                    if (clrNs.Length > rootNamespace.Length)
                    { 
                        clrNs = clrNs.Substring(rootNamespace.Length + 1);
                    }
                    else
                    { 
                        clrNs = string.Empty;
                    } 
                } 
                if (string.IsNullOrEmpty(clrNs))
                { 
                    clrTypeName = name;
                }
                else
                { 
                    clrTypeName = string.Format(CultureInfo.InvariantCulture, "{0}.{1}", clrNs, name);
                } 
                return true; 
            }
            else 
            {
                if (xamlType.TypeArguments != null && xamlType.TypeArguments.Count > 0)
                {
                    if (GetClrTypeNameFromReflection(xamlType, xamlType.TypeArguments.Count, out clrTypeName)) 
                        return true;
                } 
            } 
            clrTypeName = null;
            return false; 
        }

        static bool GetClrTypeNameFromReflection(XamlType typeName, int argumentCount, out string clrTypeName)
        { 
            clrTypeName = null;
            string @namespace = typeName.PreferredXamlNamespace; 
            string xmlnsDefAttributeName = typeof(System.Windows.Markup.XmlnsDefinitionAttribute).FullName; 
            string xmlnsDefAttributeAssem = typeof(System.Windows.Markup.XmlnsDefinitionAttribute).Assembly.GetName().Name;
 
            var matchingAssemblies =
                //
                // For each reflection-only assembly in the current application domain...
                // 
                from assembly in AppDomain.CurrentDomain.ReflectionOnlyGetAssemblies()
                // 
                // ...go through all the XmlnsDefinitionAttributes in that assembly... 
                //
                from cad in assembly.GetCustomAttributesData() 
                where cad.Constructor.DeclaringType.FullName == xmlnsDefAttributeName &&
                      cad.Constructor.DeclaringType.Assembly.GetName().Name == xmlnsDefAttributeAssem
                //
                // ... that match the specified namespace... 
                //
                where (@namespace == (string)cad.ConstructorArguments[0].Value) 
                // 
                // ... and return an {assembly, clr namespace} pair.
                // 
                select new
                {
                    Assembly = assembly,
                    Namespace = ((string)cad.ConstructorArguments[1].Value) + "." 
                };
 
            foreach (var match in matchingAssemblies) 
            {
                string argumentQualifiedName = argumentCount > 0 ? typeName.Name + "`" + argumentCount : typeName.Name; 
                Type type = match.Assembly.GetType(match.Namespace + argumentQualifiedName, false);

                if (type != null)
                { 
                    clrTypeName = type.FullName;
                    return true; 
                } 
            }
            return false; 
        }

        // Returns true if type namespace is clr-namespace and populates assemblyName
        // Else returns false with assemblyName null; 
        internal static bool GetTypeNameInAssemblyOrNamespace(XamlType type, string localAssemblyName, string realAssemblyName,
            out string typeName, out string assemblyName, out string ns) 
        { 
            int assemblyIndex, nsIndex;
            string typeNs = type.PreferredXamlNamespace; 
            typeName = GetFullTypeNameWithoutNamespace(type);
            if (IsClrNamespaceUri(typeNs, out nsIndex, out assemblyIndex))
            {
                assemblyName = assemblyIndex > -1 ? typeNs.Substring(assemblyIndex) : String.Empty; 
                if ((!string.IsNullOrEmpty(localAssemblyName) && assemblyName.Contains(localAssemblyName)) || string.IsNullOrEmpty(assemblyName))
                { 
                    assemblyName = realAssemblyName; 
                }
                int nsLength = typeNs.IndexOf(';') - nsIndex; 
                if (nsLength > 0)
                {
                    ns = typeNs.Substring(nsIndex, nsLength);
                } 
                else
                { 
                    ns = typeNs.Substring(nsIndex); 
                }
                return true; 
            }
            else
            {
                assemblyName = null; 
                ns = typeNs;
                return false; 
            } 
        }
 
        internal static string GetTypeName(XamlType type, string localAssemblyName, string realAssemblyName)
        {
            string typeName, assemblyName, ns;
            if (GetTypeNameInAssemblyOrNamespace(type, localAssemblyName, realAssemblyName, out typeName, out assemblyName, out ns)) 
            {
                return ns + "." + typeName; 
            } 
            else
            { 
                return ns + ":" + typeName;
            }
        }
 
        // Returns false if the type is known else returns true.
        internal static bool GetUnresolvedLeafTypeArg(XamlType type, ref IList unresolvedLeafTypeList) 
        { 
            if (unresolvedLeafTypeList != null && type != null && type.IsUnknown)
            { 
                if (!type.IsGeneric)
                {
                    unresolvedLeafTypeList.Add(type);
                } 
                else
                { 
                    bool hasUnknownChildren = false; 
                    foreach (XamlType typeArg in type.TypeArguments)
                    { 
                        hasUnknownChildren |= GetUnresolvedLeafTypeArg(typeArg, ref unresolvedLeafTypeList);
                    }
                    if (!hasUnknownChildren)
                    { 
                        unresolvedLeafTypeList.Add(type);
                    } 
                } 
                return true;
            } 
            else
            {
                return false;
            } 
        }
    } 
} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK