c# - nBuilder 测试数据生成器和反射

标签 c# reflection entity-framework-4 test-data

我正在使用 nBuilder 为我的应用程序生成一些测试数据。 首先,我对其进行了测试,它运行良好。 一个简单的例子:

Customer customer = Builder<Customer>
                   .CreateNew()
                   .Build();

创建一个对象并自动填充所有属性。 例如,如果客户包含属性:name,它将填充为name1 等等……

所有这一切都很好,但我现在很难动态地完成整个事情。

我现在正在做的是反射,我正在遍历我的类中的所有实体,并且应该为它们中的每一个生成一些测试数据,甚至应该填充查找和子列表,但这不是问题..我的问题是,我如何将以上 代码与any 类型一起使用?

ANYTYPE object = Builder<ANYTYPE> ...

我尝试过的:

object entity = null; //The object/Entity
Assembly assembly = Assembly.GetAssembly(typeof(EMI_ERPContext)); //Getting Assembly
Type type = assembly.GetType(entityName); //I know the Type
//entity = Activator.CreateInstance(type); Do I must create an Instance here?
object entity = Builder<dynamic> //The above code.. Tried to put dynamic as Type, but doesnt work
               .CreateNew()
               .Build();

最佳答案

我用一个控制台应用程序(在此处完成)进行了测试,伪造了 nBuilder 的类/接口(interface)/方法。

所以这可行,但没有在真实环境中尝试过。

您可以重用的方法是“TryToReflectBuilder”。它可能不那么冗长,但我使用“逐步”代码,因为它可能更明确。 ReflectionConsole.Test 用作“要反射(reflect)的实体”。

namespace ReflectionConsole {
    class Program {
        static void Main(string[] args)
        {

            object test = TryToReflectBuilder("ReflectionConsole.Test");
            Console.ReadKey();
        }

        public static object TryToReflectBuilder(string type)
        {
            //getting the assembly : not same as your way, but... that wasn't a problem for you
            var assembly = Assembly.GetAssembly(typeof(Test));

            //getting the entityType by name.
            var entityType = assembly.GetType(type);

            //The interesting (I hope) part is starting (yeah)
            //get the Builder<T> type
            var builderClassType = typeof(Builder<>);

            //create generic argument for Builder<T> will take the type of our entity (always an array)
            Type[] args = {entityType};

            //pass generic arguments to Builder<T>. Which becomes Builder<entityType>
            var genericBuilderType = builderClassType.MakeGenericType(args);

            //create a new instance of Builder<entityType>
            var builder = Activator.CreateInstance(genericBuilderType);

            //retrieve the "CreateNew" method, which belongs to Builder<T> class
            var createNewMethodInfo = builder.GetType().GetMethod("CreateNew");

            //invoke "CreateNew" from our builder instance which gives us an ObjectBuilder<T>, so now an ObjectBuilder<entityType> (well as an ISingleObjectBuilder<entityType>, but... who minds ;))
            var objectBuilder = createNewMethodInfo.Invoke(builder, null);

            //retrieve the "Build" method, which belongs to ObjectBuilder<T> class
            var buildMethodInfo = objectBuilder.GetType().GetMethod("Build");

            //finally, invoke "Build" from our ObjectBuilder<entityType> instance, which will give us... our entity !
            var result = buildMethodInfo.Invoke(objectBuilder, null);

            //it would be sad to return nothing after all these efforts, no ??
            return result;
        }
    }

    public  class Builder<T>
    {
        public static ISingleObjectBuilder<T> CreateNew()
        {
            Console.WriteLine(string.Format("{0} creating new",typeof(T)));
            return new ObjectBuilder<T>();
        } 
    }

    public interface ISingleObjectBuilder<T> : IBuildable<T>
    {
    }
    public interface IObjectBuilder<T> : ISingleObjectBuilder<T>
    {

    }
    public interface IBuildable<T>
    {
        T Build();
    }

    public class ObjectBuilder<T> : ISingleObjectBuilder<T>
    {
        public T Build()
        {
            Console.WriteLine(string.Format("{0} building myself", typeof(T)));
            return Activator.CreateInstance<T>();
        }
    }

    public class Test
    {
    }
}

关于c# - nBuilder 测试数据生成器和反射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10598203/

相关文章:

c# - 寻找一种快速简便的方法来合并 POCO 上的所有属性

c# - ObjectSet 包装器不适用于 linqToEntities 子查询

asp.net-mvc-3 - EF 和存储库模式 - 最终在一个 Controller 中出现多个 DbContext - 有任何问题(性能、数据完整性)吗?

c# - VS 调试器无法按预期工作

c# - 使用 docker-compose 使用 dotnet 核心的 MongoDB 连接超时

.net - 从反射属性中检索反射类型的值

c# - 从 FieldInfo 获取容器类实例

entity-framework - 更改表和列名称映射 Entity Framework v4.3

c# - 解析为 boolean 值或检查字符串值

c# - 为什么我不能像添加服务引用那样使用任务生成代理?