private void InitalizeFromProject(IDesignerHost host)
        {
            Debug.Assert(host != null, "No designerhost found for ShellLicenseManager");

            ILicenseReaderWriterService licrwService = (ILicenseReaderWriterService)host.GetService(typeof(ILicenseReaderWriterService));
            TextReader licxReader = licrwService.GetLicenseReader();

            if (licxReader == null)
            {
                return;
            }

            string line;

            while ((line = licxReader.ReadLine()) != null)
            {
                Type t = host.GetType(line);
                if (t == null)
                {
                    Debug.WriteLineIf(Switches.LICMANAGER.TraceVerbose, "Could not resolve type" + line);
                    continue;
                }

                AddLicensedComponent(t);
            }
        }
예제 #2
0
            /// <summary>
            ///      This retrieves our extensibility object for this document.  Our root
            ///      extensibility object is IDesignerHost.
            /// </summary>
            /// <param name='propName'>
            ///     The name of the extensibility object to retrieve.  We only understand
            ///     one:  LOGVIEWID_Designer.
            /// </param>
            /// <seealso cref='Microsoft.VisualStudio.Interop.IVsExtensibleObject'/>
            object IVsExtensibleObject.GetAutomationObject(String propName)
            {
                // Default : null or LOGVIEW_Designer
                //
                if (propName == null || String.Compare(propName, LOGVIEWID_Designer.ToString(), false, CultureInfo.InvariantCulture) == 0)
                {
                    return(host);
                }

                // Everything else, just to a host.GetService!
                //
                object obj = null;
                Type   t   = host.GetType(propName);

                if (t != null)
                {
                    obj = host.GetService(t);
                }

                if (obj == null)
                {
                    throw new COMException("", NativeMethods.E_NOINTERFACE);
                }
                return(obj);
            }
        // 

        protected override IComponent[] CreateComponentsCore(IDesignerHost host)
        {
            Type typeOfComponent = GetType(host, AssemblyName, TypeName, true);

            if (typeOfComponent == null && host != null)
                typeOfComponent = host.GetType(TypeName);

            if (typeOfComponent == null)
            {
                ITypeProviderCreator tpc = null;
                if (host != null)
                    tpc = (ITypeProviderCreator)host.GetService(typeof(ITypeProviderCreator));
                if (tpc != null)
                {
                    System.Reflection.Assembly assembly = tpc.GetTransientAssembly(this.AssemblyName);
                    if (assembly != null)
                        typeOfComponent = assembly.GetType(this.TypeName);
                }

                if (typeOfComponent == null)
                    typeOfComponent = GetType(host, AssemblyName, TypeName, true);
            }

            ArrayList comps = new ArrayList();
            if (typeOfComponent != null)
            {
                if (typeof(IComponent).IsAssignableFrom(typeOfComponent))
                    comps.Add(TypeDescriptor.CreateInstance(null, typeOfComponent, null, null));
            }

            IComponent[] temp = new IComponent[comps.Count];
            comps.CopyTo(temp, 0);
            return temp;
        }
예제 #4
0
        private CodeDomSerializer GetSerializer(IDesignerSerializationManager manager)
        {
            var attribute = TypeDescriptor.GetAttributes(typeof(System.ComponentModel.Component))[typeof(RootDesignerSerializerAttribute)] as RootDesignerSerializerAttribute;

            if (attribute == null)
            {
                return(null);
            }

            IDesignerHost host = manager.GetService(typeof(IDesignerHost)) as IDesignerHost;

            if (host == null)
            {
                return(null);
            }

            Type type = host.GetType(attribute.SerializerTypeName);

            if (type == null)
            {
                return(null);
            }

            return(Activator.CreateInstance(type) as CodeDomSerializer);
        }
예제 #5
0
        static string GetTargetNamespace(IDesignerHost dh)
        {
            var _loader = dh.GetType().GetField("_loader", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(dh);
            var CodeDom = _loader.GetType().GetProperty("CodeDom", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(_loader, null);

            var TypeNamespace = (CodeNamespace)CodeDom.GetType().GetProperty("TypeNamespace", BindingFlags.Public | BindingFlags.Instance).GetValue(CodeDom, null);

            return(TypeNamespace.Name);
        }
예제 #6
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="itemType"></param>
 /// <returns></returns>
 protected override object CreateInstance(Type itemType)
 {
     if (itemType.BaseType.Name.EndsWith("Element"))
     {
         // need to pull the editor to override the default ctor behavior
         IDesignerHost host = this.GetService(typeof(IDesignerHost)) as IDesignerHost;
         if (host != null)
         {
             editor = host.GetType().GetField("editor", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(host) as IHtmlEditor;
         }
         return(Activator.CreateInstance(itemType, editor));
     }
     else
     {
         return(base.CreateInstance(itemType));
     }
 }
예제 #7
0
        //

        protected override IComponent[] CreateComponentsCore(IDesignerHost host)
        {
            Type typeOfComponent = GetType(host, AssemblyName, TypeName, true);

            if (typeOfComponent == null && host != null)
            {
                typeOfComponent = host.GetType(TypeName);
            }

            if (typeOfComponent == null)
            {
                ITypeProviderCreator tpc = null;
                if (host != null)
                {
                    tpc = (ITypeProviderCreator)host.GetService(typeof(ITypeProviderCreator));
                }
                if (tpc != null)
                {
                    System.Reflection.Assembly assembly = tpc.GetTransientAssembly(this.AssemblyName);
                    if (assembly != null)
                    {
                        typeOfComponent = assembly.GetType(this.TypeName);
                    }
                }

                if (typeOfComponent == null)
                {
                    typeOfComponent = GetType(host, AssemblyName, TypeName, true);
                }
            }

            ArrayList comps = new ArrayList();

            if (typeOfComponent != null)
            {
                if (typeof(IComponent).IsAssignableFrom(typeOfComponent))
                {
                    comps.Add(TypeDescriptor.CreateInstance(null, typeOfComponent, null, null));
                }
            }

            IComponent[] temp = new IComponent[comps.Count];
            comps.CopyTo(temp, 0);
            return(temp);
        }
        protected override IComponent[] CreateComponentsCore(IDesignerHost host)
        {
            Type c = this.GetType(host, base.AssemblyName, base.TypeName, true);

            if ((c == null) && (host != null))
            {
                c = host.GetType(base.TypeName);
            }
            if (c == null)
            {
                ITypeProviderCreator service = null;
                if (host != null)
                {
                    service = (ITypeProviderCreator)host.GetService(typeof(ITypeProviderCreator));
                }
                if (service != null)
                {
                    Assembly transientAssembly = service.GetTransientAssembly(base.AssemblyName);
                    if (transientAssembly != null)
                    {
                        c = transientAssembly.GetType(base.TypeName);
                    }
                }
                if (c == null)
                {
                    c = this.GetType(host, base.AssemblyName, base.TypeName, true);
                }
            }
            ArrayList list = new ArrayList();

            if ((c != null) && typeof(IComponent).IsAssignableFrom(c))
            {
                list.Add(TypeDescriptor.CreateInstance(null, c, null, null));
            }
            IComponent[] array = new IComponent[list.Count];
            list.CopyTo(array, 0);
            return(array);
        }
 protected override IComponent[] CreateComponentsCore(IDesignerHost host)
 {
     Type c = this.GetType(host, base.AssemblyName, base.TypeName, true);
     if ((c == null) && (host != null))
     {
         c = host.GetType(base.TypeName);
     }
     if (c == null)
     {
         ITypeProviderCreator service = null;
         if (host != null)
         {
             service = (ITypeProviderCreator) host.GetService(typeof(ITypeProviderCreator));
         }
         if (service != null)
         {
             Assembly transientAssembly = service.GetTransientAssembly(base.AssemblyName);
             if (transientAssembly != null)
             {
                 c = transientAssembly.GetType(base.TypeName);
             }
         }
         if (c == null)
         {
             c = this.GetType(host, base.AssemblyName, base.TypeName, true);
         }
     }
     ArrayList list = new ArrayList();
     if ((c != null) && typeof(IComponent).IsAssignableFrom(c))
     {
         list.Add(TypeDescriptor.CreateInstance(null, c, null, null));
     }
     IComponent[] array = new IComponent[list.Count];
     list.CopyTo(array, 0);
     return array;
 }
        private void generateDataset(string datasetFilename, string datasetClassName)
        {
            if (datasetFilename == null || datasetFilename == "")
            {
                throw new Exception("No schema filename specified");
            }
            if (this.checkedListBoxAdapters.CheckedItems.Count == 0)
            {
                throw new Exception("No data adapter items selected.");
            }
            int extensionStart = datasetFilename.LastIndexOf('.');

            if (extensionStart == -1)
            {
                throw new Exception("Invalid data set filename " + extensionStart);
            }
            string datasetName = datasetFilename.Substring(0, extensionStart);
            VirtuosoDataAdapter adapter;

            DataSet targetDataSet = new DataSet();
            DataSet sourceDataSet;

            for (int index = 0; index < checkedListBoxAdapters.CheckedItems.Count; index++)
            {
                adapter = null;
                foreach (IComponent c in _host.Container.Components)
                {
                    if (c is VirtuosoDataAdapter &&
                        c.ToString() == this.checkedListBoxAdapters.CheckedItems[index].ToString())
                    {
                        adapter = (VirtuosoDataAdapter)c;
                        break;
                    }
                }
                if (adapter == null)
                {
                    throw new Exception("Inconsistency, failed to locate data adapter");
                }
                sourceDataSet = new DataSet();
                try
                {
                    adapter.MissingSchemaAction = MissingSchemaAction.Add;
                    if (adapter.MissingMappingAction == MissingMappingAction.Error)
                    {
                        adapter.MissingMappingAction = MissingMappingAction.Ignore;
                    }
                    DataTable [] tables = adapter.FillSchema(sourceDataSet, SchemaType.Mapped);
                    //
                    //  Set all string columns to length -1 so that the xml schema file
                    //  generates correctly
                    //
                    foreach (DataTable table in tables)
                    {
                        foreach (DataColumn column in table.Columns)
                        {
                            if (column.DataType == typeof(string))
                            {
                                column.MaxLength = -1;
                            }
                        }
                    }
                    //
                    //  Merge data table into main data set
                    //
                    foreach (DataTable table in tables)
                    {
                        if (!targetDataSet.Tables.Contains(table.TableName))
                        {
                            targetDataSet.Merge(table);
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            //
            //  Locate project items collection where .xsd file should be added
            //
            if (_projectItem == null || _projectItem.Collection == null)
            {
                throw new ApplicationException("No project information available. Unable to generate data set file.");
            }
            object       parent       = _projectItem.Collection.Parent;
            ProjectItems projectItems = null;

            while (parent != null)
            {
                if ((parent as Project) != null)
                {
                    //
                    //  The parent was the project node!
                    //
                    projectItems = ((Project)parent).ProjectItems;
                    break;
                }
                if ((parent as ProjectItem) == null)
                {
                    //
                    //  Parent node was not a project and not a project item...
                    //
                    break;
                }
                if (string.Compare(((ProjectItem)parent).Kind, EnvDTE.Constants.vsProjectItemKindPhysicalFolder, true, System.Globalization.CultureInfo.InvariantCulture) != 0)
                {
                    //
                    //  Add .xsd file to folder
                    //
                    projectItems = ((ProjectItem)parent).ProjectItems;
                    break;
                }
                parent = ((ProjectItem)parent).Collection.Parent;
            }
            Type xsdType = null;

            if (datasetClassName != null)
            {
                xsdType = _host.GetType(datasetClassName);
            }
            //
            //  Now merge the existing data set into the target
            //
            if (datasetClassName != null && xsdType != null)
            {
                ConstructorInfo constructor = xsdType.GetConstructor(new Type[] {});
                if (constructor != null)
                {
                    sourceDataSet = (DataSet)constructor.Invoke(null);
                    if (sourceDataSet != null)
                    {
                        foreach (DataTable table in sourceDataSet.Tables)
                        {
                            if (!targetDataSet.Tables.Contains(table.TableName))
                            {
                                targetDataSet.Merge(table);
                            }
                        }
                    }
                }
            }
            //
            //  Calculate filename for dataset XML schema file
            //
            if (_projectItem.Properties == null)
            {
                throw new ApplicationException("Unable to locate project item properties.");
            }
            if (_projectItem.Properties.Item("FullPath") == null)
            {
                throw new ApplicationException("Unable to resolve full path of the dataset file.");
            }
            string xsdFilename = Path.Combine(Path.GetDirectoryName(
                                                  (string)_projectItem.Properties.Item("FullPath").Value), datasetFilename);
            ProjectItem xsdItem = projectItems.DTE.Solution.FindProjectItem(xsdFilename);

            if (datasetClassName == null)
            {
                if (xsdItem != null || File.Exists(xsdFilename))
                {
                    if (MessageBox.Show(this, "File " + xsdFilename + " already exists. Overwrite?", "Overwrite Schema File", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK)
                    {
                        DialogResult = DialogResult.None;
                        return;
                    }
                }
            }
            targetDataSet.Namespace   = "http://www.tempuri.org/" + datasetName + ".xsd";
            targetDataSet.DataSetName = datasetName;
            targetDataSet.WriteXmlSchema(xsdFilename);
            //
            //  Add the .xsd file to the project, unless it already existed
            //
            if (xsdItem == null)
            {
                xsdItem = projectItems.AddFromFileCopy(xsdFilename);
            }
            //
            //  Set the CustomTool property to "MSDataSetGenerator"
            //
            if (xsdItem != null && xsdItem.Properties != null)
            {
                Property custToolsProp = xsdItem.Properties.Item("CustomTool");
                if (custToolsProp != null)
                {
                    if (custToolsProp.Value.Equals(string.Empty))
                    {
                        custToolsProp.Value = "MSDataSetGenerator";
                    }
                }
                else
                {
                    VSProjectItem vsProjectItem = (VSProjectItem)xsdItem.Object;
                    vsProjectItem.RunCustomTool();
                }
            }
            //
            //  Add a component to the designer if none exists
            //
            foreach (IComponent c in _host.Container.Components)
            {
                if (c is DataSet)
                {
                    if (String.Compare(((DataSet)c).DataSetName, datasetName, true) == 0)
                    {
                        //
                        //  Found one, done!
                        //
                        return;
                    }
                }
            }
            if (xsdType == null)
            {
                //
                //  Figure out the fully qualified namespace name
                //
                Property namespaceProp = null;
                if ((parent as Project) != null)
                {
                    namespaceProp = ((Project)parent).Properties.Item("RootNamespace");
                }
                else if ((parent as ProjectItem) != null)
                {
                    namespaceProp = ((ProjectItem)parent).Properties.Item("DefaultNamespace");
                }
                if (namespaceProp != null)
                {
                    xsdType = _host.GetType(namespaceProp.Value + "." + datasetName);
                }
            }
            //
            //  Add the component to the designer
            //
            if (xsdType != null)
            {
                if (_host.Container.Components[datasetName] == null)
                {
                    _host.CreateComponent(xsdType, datasetName);
                }
                else
                {
                    _host.CreateComponent(xsdType);
                }
            }
        }
예제 #11
0
        /// <summary>
        /// This will generate an early-bound wrapper for WMI class,
        /// add it as a source file to the current project; instantiate
        /// the newly-generated type and return it as a drag component.
        /// </summary>
        /// <param name="designerHost"> </param>
        public override IComponent[] CreateDragComponents(IDesignerHost designerHost)
        {
            try
            {
                Project[] projects = VSUtils.GetProjects(GetNodeSite());
                if (projects == null)
                {
                    return(null);
                }

                //This is an assumtion that's working so far.
                //TODO: verify this is the right way to determine the startup project
                //in the solution:
                Project curProject = projects[0];

                ProjectItems projItems = curProject.ProjectItems;
                if (projItems == null)
                {
                    return(null);
                }

                string curProjSuffix = VSUtils.MapProjectGuidToSuffix(new Guid(curProject.Kind));
                if (curProjSuffix == string.Empty)
                {
                    //neither a VB nor a CS project
                    throw new Exception(WMISys.GetString("WMISE_Invalid_Project_Type_For_CodeGen"));
                }

                Guid   curProjectType       = new Guid(curProject.Kind);
                string wrapperFileName      = className + "." + curProjSuffix;
                CodeTypeDeclaration newType = null;

                if (!mgmtClassObj.GetStronglyTypedClassCode(true, true, out newType))
                {
                    throw new Exception(WMISys.GetString("WMISE_Code_Generation_Failed"));
                }

                ICodeGenerator cg = VSUtils.MapProjectGuidToCodeGenerator(curProjectType);

                //construct generated code Namespace name (in the form "System.Management.root.cimv2")
                string[] nsParts    = nsName.ToLower().Split(new Char[] { '\\' });
                string   codeNSName = "System.Management";
                for (int i = 0; i < nsParts.Length; i++)
                {
                    codeNSName += nsParts[i] + ".";
                }
                codeNSName = codeNSName.Substring(0, codeNSName.Length - 1);

                System.CodeDom.CodeNamespace cn = new System.CodeDom.CodeNamespace(codeNSName);

                // Add imports to the code
                cn.Imports.Add(new CodeNamespaceImport("System"));
                cn.Imports.Add(new CodeNamespaceImport("System.ComponentModel"));
                cn.Imports.Add(new CodeNamespaceImport("System.Management"));
                cn.Imports.Add(new CodeNamespaceImport("System.Collections"));

                // Add class to the namespace
                cn.Types.Add(newType);

                //Now create the output file
                TextWriter tw = new StreamWriter(new FileStream(Path.GetTempPath() + "\\" + wrapperFileName,
                                                                FileMode.Create));

                // And write it to the file
                cg.GenerateCodeFromNamespace(cn, tw, new CodeGeneratorOptions());

                ProjectItem newItem = projItems.AddFromFileCopy(Path.GetTempPath() + "\\" + wrapperFileName);
                if (newItem == null)
                {
                    throw new Exception(WMISys.GetString("WMISE_Could_Not_Add_File_to_Project"));
                }

                File.Delete(Path.GetTempPath() + "\\" + wrapperFileName);

                Object comp = Activator.CreateInstance(designerHost.GetType(codeNSName + "." +
                                                                            newType.Name));
                if (comp == null)
                {
                    throw new Exception(WMISys.GetString("WMISE_Could_Not_Instantiate_Management_Class"));
                }

                return(new IComponent[] { (IComponent)comp });

                //The commented-out block below implements the solution with VS custom code generator:

                /*
                 * //create a new file containing the path to the instance object
                 * string tempFileName = Path.GetTempPath() + this.className + ".wmi";
                 * StreamWriter sw = File.AppendText(tempFileName);
                 * if (sw == null)
                 * {
                 *      return null;
                 * }
                 *
                 * sw.WriteLine(WmiHelper.MakeClassPath(this.serverName,
                 *                                                                      this.nsName,
                 *                                                                      this.className));
                 *
                 * sw.Flush();
                 * sw.Close();
                 * ProjectItem newItem = projItems.AddFromFileCopy(tempFileName);
                 * if (newItem == null)
                 * {
                 *      return null;
                 * }
                 * File.Delete(tempFileName);
                 * VSUtils.SetGenerator(newItem, "WMICodeGenerator");
                 * return null;	//is this OK???
                 */
            }
            catch (Exception exc)
            {
                MessageBox.Show(WMISys.GetString("WMISE_Exception", exc.Message, exc.StackTrace));
                return(null);
            }
        }
예제 #12
0
        /// <include file='doc\InstallerDesign.uex' path='docs/doc[@for="InstallerDesign.GetProjectInstallerDocument"]/*' />
        /// <devdoc>
        /// Gets the document that contains the installers for the project.
        /// </devdoc>
        public static IDesignerHost GetProjectInstallerDocument(IComponent component)
        {
            ProjectItem currentProjItem = (ProjectItem)component.Site.GetService(typeof(ProjectItem));

            if (currentProjItem == null)
            {
                throw new Exception(SR.GetString(SR.InstallerDesign_CouldntGetService));
            }

            Project project = currentProjItem.ContainingProject;

            ProjectItems projItems         = project.ProjectItems;
            ProjectItem  installationClass = null;

            string projectKind = project.Kind;
            string projectInstallerItemName     = null;
            string projectInstallerTemplateName = null;

            if (new Guid(projectKind).Equals(CSharpProjectGuid))
            {
                // c# has a special name for the installer wizard
                projectInstallerTemplateName = "\\NewInstaller.vsz";
            }
            else
            {
                // all other project types will have the same name
                projectInstallerTemplateName = "\\Installer.vsz";
            }

            int fileCount = currentProjItem.FileCount;

            if (fileCount == 0)
            {
                throw new Exception(SR.GetString(SR.InstallerDesign_NoProjectFilename));
            }
            string extension = Path.GetExtension(currentProjItem.get_FileNames(0));

            projectInstallerItemName = "ProjectInstaller" + extension;

            try {
                installationClass = projItems.Item(projectInstallerItemName);
            }
            catch (Exception) {
                // if file isn't in the project, we'll get an ArgumentException.
            }

            // If we could't find an existing ProjectInstaller.xx, we'll try to add
            // one from the template.
            if (installationClass == null)
            {
                string templateFileName = GetItemTemplatesDir(projectKind) + projectInstallerTemplateName;
                try {
                    // AddFromTemplate will try to copy the template file into ProjectInstaller.xx.
                    // If ProjectInstaller.xx already exists, it will bring up a dialog that says:
                    //      A file with the name 'C:\myproject\ProjectInstaller.xx'
                    //      already exists.  Do you want to replace it?
                    // If you answer yes, you're good to go.  The existing file will be overwritten
                    // with the template.  If you answer no, AddFromTemplate will throw a COMException
                    // with hresult = 0x80070050 (file already exists).
                    installationClass = projItems.AddFromTemplate(templateFileName, projectInstallerItemName);
                }
                catch (COMException e) {
                    // if the errorcode is HRESULT_FROM_WIN32(ERROR_FILE_EXISTS) (which is 0x80070050)
                    // then ProjectInstaller.xx exists and the user does not want to overwrite it.
                    // We could try to use the existing file, but that might cause more problems then
                    // want to get into.  Just inform the user that we can't add the installer.
                    if (e.ErrorCode == unchecked ((int)0x80070050))
                    {
                        throw new Exception(SR.GetString(SR.InstallerDesign_UnableToAddProjectInstaller));
                    }
                    else
                    {
                        // unexpected -> bubble up
                        throw;
                    }
                }
                catch (FileNotFoundException) {
                    // The template was not found.  This probably means that the current project type
                    // doesn't provide a template for installers.  Nothing to do but report the error.
                    throw new FileNotFoundException(SR.GetString(SR.InstallerDesign_CoulndtFindTemplate), templateFileName);
                }
            }

            try {
                installationClass.Properties.Item("SubType").Value = "Component";
                Window window = installationClass.Open(vsViewKindDesigner);
                window.Visible = true;
                IDesignerHost host = (IDesignerHost)window.Object;

                // make sure the project has a reference to System.Configuration.Install.dll.
                // This is done as a side-effect of calling GetType.
                host.GetType(typeof(ComponentInstaller).FullName);

                return(host);
            } catch (Exception e) {
                throw new Exception(SR.GetString(SR.InstallerDesign_CouldntShowProjectInstaller), e);
            }
        }