zoukankan      html  css  js  c++  java
  • 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();
            }

        }

  • 相关阅读:
    初学三种神经网络(前馈,竞争,递归联想存储网络)
    链表操作,空间复杂度要求为O(1)
    单链表的回文判断(O(n)时间复杂度和O(1)的空间复杂度)
    Floyd算法小结
    排序算法Java实现(基数排序)
    排序算法Java实现(归并排序)
    排序算法Java实现(堆排序)
    排序算法Java实现(选择排序)
    排序算法Java实现(快速排序)
    排序算法Java实现(冒泡排序)
  • 原文地址:https://www.cnblogs.com/xingyukun/p/1983855.html
Copyright © 2011-2022 走看看