Click here to Skip to main content
15,897,891 members
Articles / Programming Languages / C#

A tool for making C# decorators/proxies

Rate me:
Please Sign up or sign in to vote.
4.94/5 (27 votes)
6 Dec 2008CPOL4 min read 60.8K   486   82  
Describes a small VS add-in for making decorators from existing code.
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using EnvDTE;
using EnvDTE80;

namespace Pfactor.Extras
{
  internal static class Util
  {
    #region�Methods�(6)�

    //�Public�Methods�(6)�

    public static IEnumerable<string> GetFiles(ProjectItems projectItems)
    {
      foreach (ProjectItem pi in projectItems)
      {
        string filename = pi.get_FileNames(1);
        if (filename.ToLower().EndsWith(".cs"))
          yield return filename;
        foreach (string otherFile in GetFiles(pi.ProjectItems))
          yield return otherFile;
      }
    }

    public static IEnumerable<string> GetProjectFiles(Project p)
    {
      foreach (string filename in GetFiles(p.ProjectItems))
        yield return filename;
    }

    public static string GetProjectProperty(Project project, string propertyName)
    {
      foreach (Property p in project.Properties)
      {
        if (p.Name == propertyName)
          return p.Value.ToString();
      }
      return "Not found";
    }

    public static string MemberName(string typeName)
    {
      if (string.IsNullOrEmpty(typeName)) throw new ArgumentNullException("typeName");
      if (char.IsLower(typeName[0]))
        return "_" + typeName;
      return string.Empty + char.ToLower(typeName[0]) + 
             (typeName.Length > 1 ? typeName.Substring(1) : string.Empty);
    }

    public static string RemoveEmptyLinesAndAt(string s)
    {
      StringReader sr = new StringReader(s);
      StringBuilder sb = new StringBuilder();
      string temp;
      while ((temp = sr.ReadLine()) != null)
      {
        if (temp.Trim().Length != 0)
          sb.AppendLine(temp.Replace("@", string.Empty));
      }
      return sb.ToString();
    }

    public static void SaveDataAndAddToProject(DTE2 app, Project project, string nameOfFileNoPath, string content, bool openAndReformat)
    {
      string pfn = project.FileName;
      pfn = pfn.Substring(0, pfn.LastIndexOf('\\'));
      pfn = Path.Combine(pfn, nameOfFileNoPath);

      File.WriteAllText(pfn, content);
      project.ProjectItems.AddFromFile(pfn);
      if (openAndReformat)
      {
        app.OpenFile("{7651A701-06E5-11D1-8EBD-00A0C90F26EA}", pfn).Visible = true;
        try
        {
          app.ExecuteCommand("Edit.FormatDocument", "");
        }
        catch
        {
        }
        //app.ActiveDocument.Close(vsSaveChanges.vsSaveChangesYes);
      }
    }

    #endregion�Methods�
  }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder ActiveMesa
United Kingdom United Kingdom
I work primarily with the .NET technology stack, and specialize in accelerated code production via code generation (static or dynamic), aspect-oriented programming, MDA, domain-specific languages and anything else that gets products out the door faster. My languages of choice are C# and C++, though I'm open to suggestions.

Comments and Discussions