首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >nBuilder测试器与反射

nBuilder测试器与反射
EN

Stack Overflow用户
提问于 2012-05-15 09:50:49
回答 1查看 638关注 0票数 2

我正在使用nBuilder为我的应用程序生成一些测试数据。首先,我测试了它,它工作得很好。一个简单的例子:

代码语言:javascript
复制
Customer customer = Builder<Customer>
                   .CreateNew()
                   .Build();

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

好吧,这一切都很好,但我现在有麻烦来动态地做这件事。

我现在所做的是反射,我正在遍历我类中的所有实体,它们的前面应该生成一些测试数据,甚至应该填充查找和子列表,但这不是一个问题。我的问题是,如何在(任何类型)中使用上述代码?

ANYTYPE object = Builder<ANYTYPE> ...

我试过的是:

代码语言:javascript
复制
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();
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-05-15 22:30:04

我使用控制台应用程序进行测试(在这里完成),伪造nBuilder的类/接口/方法。

因此,这是可行的,但没有在实际环境中尝试过。

您可以重用的方法是"TryToReflectBuilder“。它可能不那么冗长,但我让“一步一步”的代码,因为它可能更明确。ReflectionConsole.Test被用作“要反映的实体”。

代码语言:javascript
复制
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
    {
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10598203

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档