CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2004
    Posts
    37

    Insert portion of code from another file

    .Net 2.0

    Previously using C++, we could use #include <filename> and the contents of that file would be dropped where the #include was specified. I am looking for something similar.

    Example:
    Code:
          public void A()
          {
             string output = string.Empty;
             for (int i = 0; i < 5; ++i)
             {
    #include <somefile>
                
                output += i;
                output += "\n";
             }
             MessageBox.Show(output);
          }
    
    somefile's contents:
     i++;
    So the output should be:
    1
    3
    5


    I can do it using reflection and partial classes but I really dont want to do it this way. Its long and clumsy.

    Thanks in advance.

  2. #2
    Join Date
    May 2007
    Posts
    1,546

    Re: Insert portion of code from another file

    You can't do this.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  3. #3
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Insert portion of code from another file

    #include is not allowed in .NET. Why would you want to do that?

  4. #4
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: Insert portion of code from another file

    If you realy want, you can add an existing file as a link in Visual Studio, meaning it exist in only one copy on the disk, but is referenced multiple times from more then one projec, but why would you do it this way? Place the shared code in class in class library and reference the assembly from whereever you want.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  5. #5
    Join Date
    Jul 2004
    Posts
    37

    Re: Insert portion of code from another file

    Thanks for your replies. Its not shared code, it is code added further down the implementation line by another group. Unfortunately, #if doesnt work for this specific example either. What I am thinking we need to do is something like this (which I dont really like) :

    Code:
    // Program.cs
    namespace ConsoleApplication1
    {
       partial class Program
       {
          public void A()
          {
             string output = string.Empty;
             for (int i = 0; i < 5; ++i)
             {
                Type[] t = new Type[1];
                t.SetValue(i.GetType(), 0);
                Assembly assm = Assembly.GetExecutingAssembly();
                Type type = assm.GetType("ConsoleApplication1.Program");
                MethodInfo mi = type.GetMethod("A1", t);
                if (mi != null)
                {
                   object a = mi.Invoke(this, new object[] { i });
                   i = Convert.ToInt32(a);
                }
    
                output += i;
                output += "\n";
             }
             MessageBox.Show(output);
          }
    
          static void Main(string[] args)
          {
             Program p = new Program();
             p.A();
          }
       }
    }
    
    //CodeFile1.cs
    namespace ConsoleApplication1
    {
       partial class Program
       {
          public int A1(int i)
          {
             return ++i;
          }
       }
    }
    By doing it this way CodeFile1.cs file doesnt have to exist and can be dropped in later.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured