Beispiel #1
0
        private PropertyBag AddEnumTypeInfoInternal(ITypeInfo typeInfo)
        {
            using (var attrScope = typeInfo.CreateAttrScope())
            {
                if (attrScope.Value.typekind == TYPEKIND.TKIND_ALIAS)
                {
                    ITypeInfo refTypeInfo;
                    typeInfo.GetRefTypeInfo(unchecked ((int)attrScope.Value.tdescAlias.lpValue.ToInt64()), out refTypeInfo);

                    var node = AddEnumTypeInfoInternal(refTypeInfo);
                    if (node != null)
                    {
                        var locator = typeInfo.GetManagedName();

                        var segments = locator.Split('.');
                        if (segments.Length > 0)
                        {
                            var namespaceNode = GetOrCreateNamespaceNode(locator);
                            if (namespaceNode != null)
                            {
                                namespaceNode.SetPropertyNoCheck(segments.Last(), node);
                                return(node);
                            }
                        }
                    }
                }
                else if (attrScope.Value.typekind == TYPEKIND.TKIND_ENUM)
                {
                    var node = GetOrCreateEnumTypeInfoNode(typeInfo);
                    if (node != null)
                    {
                        var count = attrScope.Value.cVars;
                        for (var index = 0; index < count; index++)
                        {
                            using (var varDescScope = typeInfo.CreateVarDescScope(index))
                            {
                                if (varDescScope.Value.varkind == VARKIND.VAR_CONST)
                                {
                                    var name = typeInfo.GetMemberName(varDescScope.Value.memid);
                                    node.SetPropertyNoCheck(name, Marshal.GetObjectForNativeVariant(varDescScope.Value.desc.lpvarValue));
                                }
                            }
                        }

                        return(node);
                    }
                }
            }

            return(null);
        }
        private static Type GetTypeForTypeInfo(ITypeInfo typeInfo)
        {
            // ReSharper disable EmptyGeneralCatchClause

            try
            {
                int index;
                var typeLib = typeInfo.GetContainingTypeLib(out index);

                var assembly = LoadPrimaryInteropAssembly(typeLib);
                if (assembly != null)
                {
                    var name = typeInfo.GetManagedName();
                    var guid = typeInfo.GetGuid();

                    var type = assembly.GetType(name, false, true);
                    if ((type != null) && (type.GUID == guid))
                    {
                        return(type);
                    }

                    var types = assembly.GetAllTypes().ToArray();
                    if ((index >= 0) && (index < types.Length))
                    {
                        type = types[index];
                        if ((type.GUID == guid) && (type.FullName == name))
                        {
                            return(type);
                        }
                    }

                    // ReSharper disable once PossibleNullReferenceException
                    type = types.FirstOrDefault(testType => (testType.GUID == guid) && (testType.FullName.Equals(name, StringComparison.OrdinalIgnoreCase)));
                    if (type != null)
                    {
                        return(type);
                    }
                }

                return(typeInfo.GetManagedType());
            }
            catch (Exception)
            {
            }

            return(null);

            // ReSharper restore EmptyGeneralCatchClause
        }
Beispiel #3
0
        private PropertyBag GetOrCreateEnumTypeInfoNode(ITypeInfo typeInfo)
        {
            var locator = typeInfo.GetManagedName();

            var segments = locator.Split('.');

            if (segments.Length < 1)
            {
                return(null);
            }

            PropertyBag enumTypeInfoNode = this;

            foreach (var segment in segments)
            {
                PropertyBag innerNode;

                object node;
                if (!enumTypeInfoNode.TryGetValue(segment, out node))
                {
                    innerNode = new PropertyBag(true);
                    enumTypeInfoNode.SetPropertyNoCheck(segment, innerNode);
                }
                else
                {
                    innerNode = node as PropertyBag;
                    if (innerNode == null)
                    {
                        throw new OperationCanceledException(MiscHelpers.FormatInvariant("Enumeration conflicts with '{0}' at '{1}'", node.GetFriendlyName(), locator));
                    }
                }

                enumTypeInfoNode = innerNode;
            }

            return(enumTypeInfoNode);
        }