Beispiel #1
0
        public static void SaveVsComObjects(this SdkInterfaceLibraryEntities entities, Process process, object obj, string serviceName, string packageName, string typeName)
        {
            var type = obj.GetType();
            var pUnk = Marshal.GetIUnknownForObject(obj);

            foreach (var assemblyRef in Assembly.GetExecutingAssembly().GetReferencedAssemblies())
            {
                if (assemblyRef.Name.StartsWith("Microsoft.VisualStudio.", "EnvDTE"))
                {
                    var assembly = Assembly.Load(assemblyRef);

                    foreach (var refType in assembly.GetTypes().Where(t => t.IsInterface && t.HasCustomAttribute <GuidAttribute>()))
                    {
                        if (type.Implements(refType))
                        {
                            var interfaceId = entities.GetInterfaceId(refType);

                            if (serviceName != null)
                            {
                                var tblService = entities.SaveIfNotExists <tblService>(s => s.ServiceName == serviceName, () =>
                                {
                                    return(new tblService
                                    {
                                        ServiceId = Guid.NewGuid(),
                                        ServiceName = serviceName,
                                        ComponentId = entities.CreateComponent(type, Guid.Empty, process, obj).ComponentId
                                    });
                                });
                            }
                            else if (packageName != null)
                            {
                                Debugger.Break();
                            }
                        }
                        else
                        {
                            foreach (var guidAttr in refType.GetCustomAttributes <GuidAttribute>())
                            {
                                var    guid = Guid.Parse(guidAttr.Value);
                                IntPtr ppv;

                                if (Marshal.QueryInterface(pUnk, ref guid, out ppv) == VSConstants.S_OK)
                                {
                                    var tblService = entities.SaveIfNotExists <tblService>(s => s.ServiceName == serviceName, () =>
                                    {
                                        return(new tblService
                                        {
                                            ServiceId = Guid.NewGuid(),
                                            ServiceName = serviceName,
                                            ComponentId = entities.CreateComponent(type, Guid.Empty, process, obj).ComponentId
                                        });
                                    });
                                }
                            }
                        }
                    }
                }
            }
        }
Beispiel #2
0
        public static void SaveServices(this SdkInterfaceLibraryEntities entities, Process process, Microsoft.VisualStudio.OLE.Interop.IServiceProvider serviceProvider)
        {
            var executingAssembly = Assembly.GetExecutingAssembly();

            foreach (var assemblyRef in executingAssembly.GetReferencedAssemblies())
            {
                if (assemblyRef.Name.StartsWith("Microsoft.VisualStudio.", "EnvDTE."))
                {
                    var assembly = Assembly.Load(assemblyRef);

                    foreach (var refType in assembly.GetTypes().Where(t => t.IsInterface && t.HasCustomAttribute <GuidAttribute>()))
                    {
                        var    attr         = refType.GetCustomAttribute <GuidAttribute>();
                        var    guid         = Guid.Parse(attr.Value);
                        var    IID_IUnknown = new Guid("00000000-0000-0000-C000-000000000046");
                        IntPtr pUnk;

                        if (ErrorHandler.Succeeded(serviceProvider.QueryService(ref guid, ref IID_IUnknown, out pUnk)))
                        {
                            var serviceName = GetServiceFromGuid(guid);
                            var obj         = Marshal.GetObjectForIUnknown(pUnk);
                            var tblService  = entities.SaveIfNotExists <tblService>(s => s.ServiceName == serviceName, () =>
                            {
                                return(new tblService
                                {
                                    ServiceId = Guid.NewGuid(),
                                    ServiceName = serviceName,
                                    ComponentId = entities.CreateComponent(refType, Guid.Empty, process, obj).ComponentId
                                });
                            });

                            entities.SaveVsComObjects(process, obj, serviceName, string.Empty, string.Empty);
                        }
                    }
                }
            }
        }