Created
November 21, 2011 02:12
-
-
Save masaru-b-cl/1381411 to your computer and use it in GitHub Desktop.
PropertyUtils.copyProperties的な何か
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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