mock class generator

根据接口的定义,自动生成符合这个接口的mock class。

class Program
    {
        
private static string assemblyFullName;
        
private static string interfaceName;
        
private static string outputFileName;

        
static void Main(string[] args)
        {
            assemblyFullName 
= @"C:\IGT_0803\development\dev_sbx_41\EGS\sb\lib\AnyCpu\debug\IGT.SB.PTN.Contracts.dll";
            interfaceName 
= "IGT.SB.PTN.Contracts.IPTNG2SHostServices";
            outputFileName 
= @"C:\Users\xingy\Desktop\mock.cs";

            StringBuilder totalContent 
= new StringBuilder();
            StringBuilder methodBody 
= new StringBuilder();
            StringBuilder extraMembers 
= new StringBuilder();
            Assembly ass 
= Assembly.LoadFrom(assemblyFullName);
            Type tt 
= ass.GetType(interfaceName);
            MethodInfo[] miArray 
= tt.GetMethods();

            
foreach (MethodInfo info in miArray)
            {
                methodBody.Clear();
                extraMembers.Clear();
                ParameterInfo returnParam 
= info.ReturnParameter;
                
bool isReturnVoid = returnParam.ParameterType.FullName.Equals("System.Void",
                                                                              StringComparison.
                                                                                  InvariantCultureIgnoreCase)
                                        
? true
                                        : 
false;

                methodBody.Append(
"public ");
                methodBody.Append(returnParam 
+ " ");
                methodBody.Append(info.Name).Append(
"(");

                
string nameOfIsCalled = "Is" + info.Name + "Called";
                extraMembers.Append(
"public bool ")
                    .Append(nameOfIsCalled)
                    .Append(
";")
                    .Append(Environment.NewLine);

                
if (!isReturnVoid)
                {
                    extraMembers.Append(
"public ")
                        .Append(returnParam.ParameterType)
                        .Append(
" ").Append(info.Name).Append("_actualReturnValue")
                        .Append(Environment.NewLine);
                }

                ParameterInfo[] paras 
= info.GetParameters();

                
foreach (ParameterInfo parameterInfo in paras)
                {
                    extraMembers.Append(
"public ")
                    .Append(parameterInfo.ParameterType.ToString()).Append(
" ")
                    .Append(parameterInfo.Name).Append(
"_actualValue")
                    .Append(
";")
                    .Append(Environment.NewLine);
                }

                totalContent.Append(extraMembers.ToString());

                
foreach (ParameterInfo parameterInfo in paras)
                {
                    methodBody.Append(parameterInfo.ParameterType.ToString())
                        .Append(
" ")
                        .Append(parameterInfo.Name);
                }

                methodBody.Append(
")").Append(Environment.NewLine);
                methodBody.Append(
"{").Append(Environment.NewLine);

                methodBody.Append(
"  ")
                    .Append(nameOfIsCalled)
                    .Append(
" = true;")
                    .Append(Environment.NewLine);

                methodBody.Append(Environment.NewLine);

                
foreach (ParameterInfo parameterInfo in paras)
                {
                    methodBody
                        .Append(
"  ")
                        .Append(parameterInfo.Name)
                        .Append(
"_actualValue")
                        .Append(
" = ")
                        .Append(parameterInfo.Name)
                        .Append(
";")
                        .Append(Environment.NewLine);
                }

                
if (!isReturnVoid)
                {
                    methodBody
                        .Append(
"  ")
                        .Append(
"return ")
                        .Append(info.Name).Append(
"_actualReturnValue")
                        .Append(
";")
                        .Append(Environment.NewLine);
                }

                methodBody.Append(
"}").Append(Environment.NewLine);

                totalContent.Append(methodBody.ToString()).Append(Environment.NewLine);

            }
            Console.WriteLine(totalContent);
            File.WriteAllText(outputFileName, totalContent.ToString());
            Console.Read();
        }

    }

posted @ 2011-03-14 15:34  EagleFish(邢瑜琨)  阅读(353)  评论(0编辑  收藏  举报