Skip to content

Instantly share code, notes, and snippets.

@masaru-b-cl
Created November 21, 2011 02:12
Show Gist options
  • Save masaru-b-cl/1381411 to your computer and use it in GitHub Desktop.
Save masaru-b-cl/1381411 to your computer and use it in GitHub Desktop.
PropertyUtils.copyProperties的な何か
using System;
using System.Linq;
using System.Reflection;
namespace PropertyReflectionConsoleApplication
{
public class Entity
{
public Entity()
{
}
public Entity(int count)
{
this.Count = count;
}
public string Code { get; set; }
private int Count { get; set; }
public string this[string propertyName]
{
get
{
return propertyName;
}
}
public override string ToString()
{
return String.Format("Code : {0}, Count : {1}", Code, Count);
}
}
public class Program
{
public static void Main(string[] args)
{
var src = new Entity(10) { Code = "100" };
var propInfos = src.GetType().GetProperties(
BindingFlags.Instance
| BindingFlags.Public
| BindingFlags.NonPublic)
.Where(pi => pi.GetIndexParameters().Length == 0)
;
var dest = new Entity();
foreach (var pi in propInfos)
{
pi.SetValue(dest, pi.GetValue(src, null), null);
}
Console.WriteLine(dest);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment