示例#1
0
 // clear info read from file
 private void _clearInfo()
 {
     _aTypes             = new Dictionary <string, Type>(StringComparer.OrdinalIgnoreCase);
     _aExtensions        = new List <MethodInfo>();
     _nameSpaces         = new Hashtable(StringComparer.OrdinalIgnoreCase);
     _nameSpaceTexts     = new List <string>();
     _implicitNamespaces = new List <string>();
     _zeroNamespace      = new NameSpaceContainer("_");
     _LoadedTypes        = false;
     _HasExtensions      = false;
 }
示例#2
0
        internal void UpdateAssembly()
        {
            if (this._failed > 3)
            {
                return;
            }
            Type[] types       = null;
            var    aTypes      = new Dictionary <string, Type>(StringComparer.OrdinalIgnoreCase);
            var    aExtensions = new List <MethodInfo>();
            //
            int    num;
            string nspace     = "";
            string fullName   = "";
            string simpleName = "";

            //
            this._nameSpaces.Clear();
            this._nameSpaceTexts.Clear();

            this._zeroNamespace.Clear();
            this._globalClassName = "";
            this._HasExtensions   = false;
            //
            this.LoadAssembly();
            try
            {
                if (_assembly != null)
                {
                    object[] customAttributes = _assembly.GetCustomAttributes(false);
                    for (num = 1; num <= customAttributes.Length; num++)
                    {
                        object custattr = customAttributes[num - 1];
                        Type   type     = custattr.GetType();
                        switch (custattr.ToString())
                        {
                        case "Vulcan.Internal.VulcanClassLibraryAttribute":
                            this._globalClassName = type.GetProperty("globalClassName").GetValue(custattr, null).ToString();
                            //
                            string defaultNS = type.GetProperty("defaultNamespace").GetValue(custattr, null).ToString();
                            if (!string.IsNullOrEmpty(defaultNS))
                            {
                                this._implicitNamespaces.Add(defaultNS);
                            }
                            break;

                        case "System.Runtime.CompilerServices.ExtensionAttribute":
                            this._HasExtensions = true;
                            break;

                        case "Vulcan.VulcanImplicitNamespaceAttribute":
                            string nameS = type.GetProperty("Namespace").GetValue(custattr, null).ToString();
                            if (!string.IsNullOrEmpty(nameS))
                            {
                                this._implicitNamespaces.Add(nameS);
                            }
                            break;
                        }
                    }
                }
            }
            catch
            {
            }
            // Load Types From Assembly, if possible
            // register event handler to find missing assemblies
            AppDomain currentDomain = AppDomain.CurrentDomain;

            currentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
            try
            {
                if (_assembly != null)
                {
                    types = _assembly.GetTypes();
                }
                this._failed = 0;
            }
            catch (ReflectionTypeLoadException e)
            {
                Support.Debug("Cannot load types from {0}", _assembly.GetName().Name);
                Support.Debug("Exception details:");
                string lastMsg = null;
                foreach (var le in e.LoaderExceptions)
                {
                    if (le.Message != lastMsg)
                    {
                        Support.Debug(le.Message);
                        lastMsg = le.Message;
                    }
                }
                Support.Debug("Types loaded:");
                foreach (var t in e.Types)
                {
                    if (t != null)
                    {
                        Support.Debug(t.FullName);
                    }
                }
                _assembly     = null;
                this._failed += 1;
            }
            catch (Exception e)
            {
                Support.Debug("Generic exception:");
                Support.Debug(e.Message);
            }
            // Has Types ?
            currentDomain.AssemblyResolve -= CurrentDomain_AssemblyResolve;
            if (types?.Length > 0 && (aTypes?.Count == 0 | !_LoadedTypes))
            {
                try
                {
                    for (num = 1; num <= types.Length; num++)
                    {
                        // First, Get Fullname ( for eg, System.Collections.Generic.List`1 )
                        fullName = types[num - 1].FullName;
                        // Remove "special" Types
                        if (fullName.StartsWith("$") || fullName.StartsWith("<"))
                        {
                            continue;
                        }
                        //
                        if (this._HasExtensions && HasExtensionAttribute(types[num - 1]))
                        {
                            MethodInfo[] methods = types[num - 1].GetMethods(BindingFlags.Public | BindingFlags.Static);
                            foreach (MethodInfo info in methods)
                            {
                                if (HasExtensionAttribute(info))
                                {
                                    aExtensions.Add(info);
                                }
                            }
                        }
                        // Nested Type ?
                        if (fullName.Contains("+"))
                        {
                            fullName = fullName.Replace('+', '.');
                        }
                        // Generic ?
                        if (fullName.Contains("`"))
                        {
                            // Extract the "normal" name
                            fullName = fullName.Substring(0, fullName.IndexOf("`") + 2);
                        }
                        // Add to the FullyQualified name
                        if (!aTypes.ContainsKey(fullName))
                        {
                            aTypes.Add(fullName, types[num - 1]);
                        }
                        // Now, with Standard name
                        simpleName = types[num - 1].Name;
                        simpleName = simpleName.Replace('+', '.');
                        // Not Empty namespace, not a generic, not nested, not starting with underscore
                        if (((string.IsNullOrEmpty(types[num - 1].Namespace) && (simpleName.IndexOf('`') == -1)) && ((simpleName.IndexOf('+') == -1) && (simpleName.IndexOf('<') == -1))) && !simpleName.StartsWith("_"))
                        {
                            // Add the Name only, with the Kind
                            this._zeroNamespace.AddType(simpleName, this.GetTypeTypesFromType(types[num - 1]));
                        }
                        // Public Type, not Nested and no Underscore
                        if ((types[num - 1].IsPublic && (simpleName.IndexOf('+') == -1)) && (simpleName.IndexOf('_') == -1))
                        {
                            // Get the Namespace
                            nspace = types[num - 1].Namespace;
                            // and the normal name
                            simpleName = types[num - 1].Name;
                            simpleName = simpleName.Replace('+', '.');
                            // Generic ?
                            int index = simpleName.IndexOf('`');
                            if (index != -1)
                            {
                                simpleName = simpleName.Substring(0, index);
                            }
                            if ((nspace != null) && (nspace.Length > 0))
                            {
                                NameSpaceContainer container;
                                ;
                                if (!this._nameSpaces.ContainsKey(nspace))
                                {
                                    container = new NameSpaceContainer(nspace);
                                    container.AddType(simpleName, this.GetTypeTypesFromType(types[num - 1]));
                                    this._nameSpaces.Add(nspace, container);
                                    this._nameSpaceTexts.Add(nspace);
                                }
                                else
                                {
                                    container = (NameSpaceContainer)this._nameSpaces[nspace];
                                    container.AddType(simpleName, this.GetTypeTypesFromType(types[num - 1]));
                                }
                                while (nspace.Contains("."))
                                {
                                    nspace = nspace.Substring(0, nspace.LastIndexOf('.'));
                                    if (!this._nameSpaceTexts.Contains(nspace))
                                    {
                                        this._nameSpaceTexts.Add(nspace);
                                    }
                                }
                            }
                        }
                    }
                    // Mark as Loaded
                    this._LoadedTypes = true;
                    this._aTypes      = aTypes.ToImmutableDictionary(StringComparer.OrdinalIgnoreCase);
                    this._aExtensions = aExtensions.ToImmutableList();
                    this._failed      = 0;
                    _assembly         = null;
                }
                catch (Exception e)
                {
                    Support.Debug("Generic exception:");
                    Support.Debug(e.Message);
                    // empty values
                    _clearInfo();
                }
            }
        }