扣出MSLinqToSQLGenerator的基类,可用于开发自定义工具(custom tool)

    代码有小小整理过

 

namespace  CodeGenerator
ExpandedBlockStart.gifContractedBlock.gif
{
    
using Microsoft.VisualStudio.Shell.Interop;
    
using System;
    
using System.Runtime.InteropServices;

    
internal abstract class BaseCodeGenerator : IVsSingleFileGenerator
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
private string codeFileNameSpace = string.Empty;
        
private string codeFilePath = string.Empty;
        
private IVsGeneratorProgress codeGeneratorProgress;

        
protected BaseCodeGenerator()
ExpandedSubBlockStart.gifContractedSubBlock.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.gifContractedSubBlock.gif        
{
            
if (bstrInputFileContents == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
throw new ArgumentNullException(bstrInputFileContents);
            }

            
this.codeFilePath = wszInputFilePath;
            
this.codeFileNameSpace = wszDefaultNamespace;
            
this.codeGeneratorProgress = pGenerateProgress;
            
byte[] source = this.GenerateCode(wszInputFilePath, bstrInputFileContents);
            
if (source == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                pbstrOutputFileContents 
= null;
                pbstrOutputFileContentSize 
= 0;
            }

            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                pbstrOutputFileContentSize 
= (uint) source.Length;
                pbstrOutputFileContents[
0= Marshal.AllocCoTaskMem(source.Length);
                Marshal.Copy(source, 
0, pbstrOutputFileContents[0], source.Length);
            }

            
return 0;
        }


        
protected abstract byte[] GenerateCode(string inputFileName, string inputFileContent);
        
protected virtual void GeneratorErrorCallback(int warning, uint level, string message, uint line, uint column)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            IVsGeneratorProgress codeGeneratorProgress 
= this.CodeGeneratorProgress;
            
if (codeGeneratorProgress != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                codeGeneratorProgress.GeneratorError(warning, level, message, line, column);
            }

        }


        
internal IVsGeneratorProgress CodeGeneratorProgress
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
return this.codeGeneratorProgress;
            }

        }


        
protected string FileNameSpace
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
return this.codeFileNameSpace;
            }

        }


        
protected string InputFilePath
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
return this.codeFilePath;
            }

        }

    }

}


 

 

 

namespace  CodeGenerator
ExpandedBlockStart.gifContractedBlock.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;

    
public abstract class BaseCodeGeneratorWithSite : BaseCodeGenerator, IObjectWithSite
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
private CodeDomProvider codeDomProvider;
        
private ServiceProvider serviceProvider;
        
private object site;

        
protected BaseCodeGeneratorWithSite()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
        }


        
protected void AddReferenceDLLsToProject(ICollection<string> referenceDLLs)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
string str = AddReferenceDLLToProject(this.SiteServiceProvider, referenceDLLs);
            
if (!EmptyOrSpace(str))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
this.GeneratorErrorCallback(01, str, 00);
            }

        }


        
public static bool EmptyOrSpace(string str)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if (str != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
return (0 >= str.Trim().Length);
            }

            
return true;
        }


        
private static object GetProjectObject(Project containingProject)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
object obj2 = containingProject.Object;
            
if (IsWebProject(containingProject) && (obj2 is VSWebSite))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
return obj2;
            }

            
if (obj2 is VSProject)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
return obj2;
            }

            
return null;
        }


        
public static bool IsWebProject(Project p)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if (p == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
return false;
            }

            
return (p.Kind == "{E24C65DC-7377-472b-9ABA-BC803B73C61A}");
        }


        
private static void AddReferenceToProjectObject(object projectObj, string referenceDll)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if (projectObj != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
if (projectObj is VSWebSite)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    VSWebSite site 
= projectObj as VSWebSite;
                    site.References.AddFromGAC(referenceDll);
                }

                
else if (projectObj is VSProject)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    VSProject project 
= projectObj as VSProject;
                    project.References.Add(referenceDll);
                }

            }

        }


        [SuppressMessage(
"Microsoft.Design""CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Failure only of interest to devs")]
        
public static string AddReferenceDLLToProject(System.IServiceProvider serviceProvider, ICollection<string> referenceDLL)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
if (referenceDLL.Count == 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
return string.Empty;
                }

                
object service = serviceProvider.GetService(typeof(ProjectItem));
                
if (service == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
return "Failed to add reference dll.";
                }

                Project containingProject 
= ((ProjectItem)service).ContainingProject;
                
if (containingProject == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
return "Failed to add reference dll.";
                }

                
object projectObject = GetProjectObject(containingProject);
                
if (projectObject == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
return "Failed to add reference dll.";
                }

                IVsHierarchy hierarchy 
= (IVsHierarchy)serviceProvider.GetService(typeof(IVsHierarchy));
                IVsProjectBuildSystem system 
= hierarchy as IVsProjectBuildSystem;
                
if (system != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    system.StartBatchEdit();
                }

                
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
foreach (string str in referenceDLL)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        AddReferenceToProjectObject(projectObject, str);
                    }

                    
if (system != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        system.EndBatchEdit();
                    }

                }

                
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
if (system != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        system.CancelBatchEdit();
                    }

                    
return "Failed to add reference dll.";
                }

            }

            
catch (Exception)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
return "Failed to add reference dll.";
            }

            
return string.Empty;
        }


        
protected virtual string CreateExceptionMessage(Exception e)
ExpandedSubBlockStart.gifContractedSubBlock.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.gifContractedSubBlock.gif            
{
                
string message = exception.Message;
                
if ((message != null&& (message.Length > 0))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    builder.Append(
" ");
                    builder.Append(message);
                }

            }

            
return builder.ToString();
        }


        
public string GetGeneratedCodeFile(string inputFileName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
string extension = null;
            
this.DefaultExtension(out extension);
            
return Path.ChangeExtension(inputFileName, extension);
        }


        
public override int DefaultExtension(out string extension)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            CodeDomProvider codeProvider 
= this.CodeProvider;
            extension 
= "designer." + codeProvider.FileExtension;
            
if (((extension != null&& (extension.Length > 0)) && (extension[0!= '.'))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                extension 
= "." + extension;
            }

            
return 0;
        }


        
public byte[] StringToByteArray(string inputContents, Encoding encoding)
ExpandedSubBlockStart.gifContractedSubBlock.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();
        }


        
protected object GetService(Guid serviceGuid)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return this.SiteServiceProvider.GetService(serviceGuid);
        }


        
protected object GetService(Type serviceType)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return this.SiteServiceProvider.GetService(serviceType);
        }


        
public virtual void GetSite(ref Guid riid, out IntPtr pSite)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if (this.site == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
throw new Win32Exception();
            }

            IntPtr iUnknownForObject 
= Marshal.GetIUnknownForObject(this.site);
            pSite 
= IntPtr.Zero;
            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                Marshal.QueryInterface(iUnknownForObject, 
ref riid, out pSite);
            }

            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
if (iUnknownForObject != IntPtr.Zero)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    Marshal.Release(iUnknownForObject);
                }

            }

            
if (pSite == IntPtr.Zero)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
throw new Win32Exception();
            }

        }


        
public virtual void SetSite(object pUnkSite)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
this.site = pUnkSite;
            
this.codeDomProvider = null;
            
this.serviceProvider = null;
        }


        
public static CodeDomProvider GetCodeDomProviderFromServiceProvider(System.IServiceProvider sp)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if (sp == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
return null;
            }

            ServiceInstance instance 
= new ServiceInstance(sp);
            IVSMDCodeDomProvider iVSMDCodeDomProvider 
= instance.IVSMDCodeDomProvider;
            
if (iVSMDCodeDomProvider == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
return null;
            }

            
return (iVSMDCodeDomProvider.CodeDomProvider as CodeDomProvider);
        }


        
protected virtual CodeDomProvider CodeProvider
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
if (this.codeDomProvider == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        
this.codeDomProvider = GetCodeDomProviderFromServiceProvider(this.SiteServiceProvider);
                    }

                    
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        
this.codeDomProvider = CodeDomProvider.CreateProvider("cs");
                    }

                }

                
return this.codeDomProvider;
            }

            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
if (value == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
throw new ArgumentNullException();
                }

                
this.codeDomProvider = value;
            }

        }


        
protected ServiceProvider SiteServiceProvider
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
if (this.serviceProvider == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    Microsoft.VisualStudio.OLE.Interop.IServiceProvider site 
= this.site as Microsoft.VisualStudio.OLE.Interop.IServiceProvider;
                    
this.serviceProvider = new ServiceProvider(site);
                }

                
return this.serviceProvider;
            }

        }

    }

}


 

使用的时候重写GenerateCode方法即可

转载于:https://www.cnblogs.com/ofei/archive/2008/07/19/1246486.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值