代码有小小整理过
namespace
CodeGenerator
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
using Microsoft.VisualStudio.Shell.Interop;
using System;
using System.Runtime.InteropServices;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
internal abstract class BaseCodeGenerator : IVsSingleFileGenerator
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private string codeFileNameSpace = string.Empty;
private string codeFilePath = string.Empty;
private IVsGeneratorProgress codeGeneratorProgress;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected BaseCodeGenerator()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public abstract int DefaultExtension(out string extension);
public int Generate(string wszInputFilePath, string bstrInputFileContents, string wszDefaultNamespace, IntPtr[] pbstrOutputFileContents, out uint pbstrOutputFileContentSize, IVsGeneratorProgress pGenerateProgress)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (bstrInputFileContents == null)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new ArgumentNullException(bstrInputFileContents);
}
this.codeFilePath = wszInputFilePath;
this.codeFileNameSpace = wszDefaultNamespace;
this.codeGeneratorProgress = pGenerateProgress;
byte[] source = this.GenerateCode(wszInputFilePath, bstrInputFileContents);
if (source == null)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
pbstrOutputFileContents = null;
pbstrOutputFileContentSize = 0;
}
else
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
pbstrOutputFileContentSize = (uint) source.Length;
pbstrOutputFileContents[0] = Marshal.AllocCoTaskMem(source.Length);
Marshal.Copy(source, 0, pbstrOutputFileContents[0], source.Length);
}
return 0;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected abstract byte[] GenerateCode(string inputFileName, string inputFileContent);
protected virtual void GeneratorErrorCallback(int warning, uint level, string message, uint line, uint column)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
IVsGeneratorProgress codeGeneratorProgress = this.CodeGeneratorProgress;
if (codeGeneratorProgress != null)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
codeGeneratorProgress.GeneratorError(warning, level, message, line, column);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
internal IVsGeneratorProgress CodeGeneratorProgress
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this.codeGeneratorProgress;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected string FileNameSpace
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this.codeFileNameSpace;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected string InputFilePath
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this.codeFilePath;
}
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace
CodeGenerator
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
using Microsoft.VisualStudio.OLE.Interop;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using System.Diagnostics.CodeAnalysis;
using EnvDTE;
using VsWebSite;
using VSLangProj;
using Microsoft.VisualStudio.Designer.Interfaces;
using System.IO;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public abstract class BaseCodeGeneratorWithSite : BaseCodeGenerator, IObjectWithSite
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private CodeDomProvider codeDomProvider;
private ServiceProvider serviceProvider;
private object site;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected BaseCodeGeneratorWithSite()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected void AddReferenceDLLsToProject(ICollection<string> referenceDLLs)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string str = AddReferenceDLLToProject(this.SiteServiceProvider, referenceDLLs);
if (!EmptyOrSpace(str))
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.GeneratorErrorCallback(0, 1, str, 0, 0);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public static bool EmptyOrSpace(string str)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (str != null)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return (0 >= str.Trim().Length);
}
return true;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private static object GetProjectObject(Project containingProject)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
object obj2 = containingProject.Object;
if (IsWebProject(containingProject) && (obj2 is VSWebSite))
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return obj2;
}
if (obj2 is VSProject)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return obj2;
}
return null;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public static bool IsWebProject(Project p)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (p == null)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return false;
}
return (p.Kind == "{E24C65DC-7377-472b-9ABA-BC803B73C61A}");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private static void AddReferenceToProjectObject(object projectObj, string referenceDll)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (projectObj != null)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (projectObj is VSWebSite)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
VSWebSite site = projectObj as VSWebSite;
site.References.AddFromGAC(referenceDll);
}
else if (projectObj is VSProject)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
VSProject project = projectObj as VSProject;
project.References.Add(referenceDll);
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Failure only of interest to devs")]
public static string AddReferenceDLLToProject(System.IServiceProvider serviceProvider, ICollection<string> referenceDLL)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (referenceDLL.Count == 0)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return string.Empty;
}
object service = serviceProvider.GetService(typeof(ProjectItem));
if (service == null)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return "Failed to add reference dll.";
}
Project containingProject = ((ProjectItem)service).ContainingProject;
if (containingProject == null)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return "Failed to add reference dll.";
}
object projectObject = GetProjectObject(containingProject);
if (projectObject == null)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return "Failed to add reference dll.";
}
IVsHierarchy hierarchy = (IVsHierarchy)serviceProvider.GetService(typeof(IVsHierarchy));
IVsProjectBuildSystem system = hierarchy as IVsProjectBuildSystem;
if (system != null)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
system.StartBatchEdit();
}
try
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
foreach (string str in referenceDLL)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
AddReferenceToProjectObject(projectObject, str);
}
if (system != null)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
system.EndBatchEdit();
}
}
catch
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (system != null)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
system.CancelBatchEdit();
}
return "Failed to add reference dll.";
}
}
catch (Exception)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return "Failed to add reference dll.";
}
return string.Empty;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected virtual string CreateExceptionMessage(Exception e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
StringBuilder builder = new StringBuilder();
builder.Append((e.Message != null) ? e.Message : string.Empty);
for (Exception exception = e.InnerException; exception != null; exception = exception.InnerException)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string message = exception.Message;
if ((message != null) && (message.Length > 0))
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
builder.Append(" ");
builder.Append(message);
}
}
return builder.ToString();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public string GetGeneratedCodeFile(string inputFileName)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string extension = null;
this.DefaultExtension(out extension);
return Path.ChangeExtension(inputFileName, extension);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public override int DefaultExtension(out string extension)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
CodeDomProvider codeProvider = this.CodeProvider;
extension = "designer." + codeProvider.FileExtension;
if (((extension != null) && (extension.Length > 0)) && (extension[0] != '.'))
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
extension = "." + extension;
}
return 0;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public byte[] StringToByteArray(string inputContents, Encoding encoding)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
byte[] preamble = encoding.GetPreamble();
byte[] bytes = encoding.GetBytes(inputContents);
MemoryStream stream = new MemoryStream(preamble.Length + bytes.Length);
stream.Write(preamble, 0, preamble.Length);
stream.Write(bytes, 0, bytes.Length);
stream.Position = 0L;
return stream.ToArray();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected object GetService(Guid serviceGuid)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this.SiteServiceProvider.GetService(serviceGuid);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected object GetService(Type serviceType)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this.SiteServiceProvider.GetService(serviceType);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public virtual void GetSite(ref Guid riid, out IntPtr pSite)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (this.site == null)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new Win32Exception();
}
IntPtr iUnknownForObject = Marshal.GetIUnknownForObject(this.site);
pSite = IntPtr.Zero;
try
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Marshal.QueryInterface(iUnknownForObject, ref riid, out pSite);
}
finally
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (iUnknownForObject != IntPtr.Zero)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Marshal.Release(iUnknownForObject);
}
}
if (pSite == IntPtr.Zero)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new Win32Exception();
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public virtual void SetSite(object pUnkSite)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.site = pUnkSite;
this.codeDomProvider = null;
this.serviceProvider = null;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public static CodeDomProvider GetCodeDomProviderFromServiceProvider(System.IServiceProvider sp)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (sp == null)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return null;
}
ServiceInstance instance = new ServiceInstance(sp);
IVSMDCodeDomProvider iVSMDCodeDomProvider = instance.IVSMDCodeDomProvider;
if (iVSMDCodeDomProvider == null)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return null;
}
return (iVSMDCodeDomProvider.CodeDomProvider as CodeDomProvider);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected virtual CodeDomProvider CodeProvider
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (this.codeDomProvider == null)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.codeDomProvider = GetCodeDomProviderFromServiceProvider(this.SiteServiceProvider);
}
catch
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.codeDomProvider = CodeDomProvider.CreateProvider("cs");
}
}
return this.codeDomProvider;
}
set
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (value == null)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new ArgumentNullException();
}
this.codeDomProvider = value;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected ServiceProvider SiteServiceProvider
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (this.serviceProvider == null)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Microsoft.VisualStudio.OLE.Interop.IServiceProvider site = this.site as Microsoft.VisualStudio.OLE.Interop.IServiceProvider;
this.serviceProvider = new ServiceProvider(site);
}
return this.serviceProvider;
}
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
使用的时候重写GenerateCode方法即可
转载于:https://www.cnblogs.com/ofei/archive/2008/07/19/1246486.html