示例#1
0
    public static void InsertClassCodeIntoDotNetProxyFile(string ClassName, string ClassSourceCode)
    {
        if (ClassSourceCode != "")
        {
            ClassName = StringManip.CamelCase(ClassName);

            // Go open the proxy source file and read it's contents.
            string Contents = "";
            if (File.Exists(WriteableBaseProxyFileName))
            {
                StreamReader In = new StreamReader(WriteableBaseProxyFileName);
                Contents = In.ReadToEnd();
                In.Close();
            }
            if (Contents == "")
            {
                Contents = "using System;\r\n" +
                           "using System.Collections.Generic;\r\n" +
                           "using System.Text;\r\n" +
                           "using System.Runtime.InteropServices;\r\n" +
                           "#pragma warning disable 67 // Suppress warning messages about unused events\r\n" +
                           "namespace ModelFramework {\r\n" +
                           "}\r\n";
            }

            // See if we can find an existing class in the source code.
            int PosStartClass = Contents.IndexOf("public class " + ClassName + " ");
            if (PosStartClass != -1)
            {
                int PosStartAttribute = Contents.LastIndexOf("[ComponentType", PosStartClass);
                int PosOpenBracket    = Contents.IndexOf("{", PosStartClass);
                int PosEndClass       = StringManip.FindMatchingClosingBracket(Contents, PosStartClass, '{', '}');
                if (PosEndClass != -1)
                {
                    if (PosStartAttribute != -1)
                    {
                        PosStartClass = PosStartAttribute;
                    }
                    Contents = Contents.Remove(PosStartClass, PosEndClass - PosStartClass + 5); // also removes 2 x \r\n
                }
            }

            // Remove the last curly bracket - namespace bracket. We'll add it in later.
            int PosLastBracket = Contents.LastIndexOf('}');
            if (PosLastBracket == -1)
            {
                throw new Exception("Cannot find namespace in DotNetProxies.cs");
            }
            Contents = Contents.Remove(PosLastBracket);

            // Now add in our class and closing bracket for namespace.
            Contents = Contents + ClassSourceCode + "\r\n}";

            // Write contents back to proxy file
            StreamWriter Out = new StreamWriter(WriteableBaseProxyFileName);
            Out.Write(Contents);
            Out.Close();
        }
    }