Tynamix / ObjectFiller.NET

The .NET ObjectFiller fills the properties of your .NET objects with random data

Home Page:http://objectfiller.net

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Make copy of existing object with one or more property changes.

levirgon opened this issue · comments

I want to achieve something like this. is this possible using current APIs?

            var filler = new Filler<Student>();
            filler.Setup(true)
            .CopyFrom(existing)   // all properties are copied from an existing instance
            .OnProperty(student=>student.Name).Use(value);  //change value of one or more property explicitly

            return filler.Create();

Sorry for the late response. Filler does not copy your object. You have to do it by yourself. But you can fill instead of create a exisiting object:

using System;
using Tynamix.ObjectFiller;

namespace objectfiller
{
    class Program
    {

        public class Student
        {
            public int Age { get; set; }

            public string FirstName { get; set; }

            public string LastName { get; set; }
        }


        static void Main(string[] args)
        {
            Student student = new Student();
            student.Age =  18;
            student.FirstName = "Foo";
            student.LastName = "Bar";

            Filler<Student> filler = new Filler<Student>();
            filler.Setup(true) // this true flag means that the setup will be explicit
            .OnProperty(x=>x.FirstName).Use<RealNames>();

            filler.Fill(student); // Use Fill instead of create

            Console.WriteLine(student.FirstName);
            Console.WriteLine(student.LastName);
            Console.WriteLine(student.Age);

        }
    }
}