using System;
using System.Reflection;
public class Program {
public static void Main() {
var objeto = new Tipo() { x = 1, a = "aaa" };
var objeto2 = ObjectUtil<Tipo>.Clone(objeto);
Console.WriteLine(objeto2.x);
}
}
static class ObjectUtil<T> {
public static T Clone(T obj) {
Type type = obj.GetType();
var fields = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
var cloned = Activator.CreateInstance(type);
for (int i = 0; i < fields.Length; i++) fields[i].SetValue(cloned, fields[i].GetValue(obj));
return (T)cloned;
}
}
class Tipo {
public int x;
public string a;
public TimeZoneInfo t;
}
//https://pt.stackoverflow.com/q/177137/101
dXNpbmcgU3lzdGVtOwp1c2luZyBTeXN0ZW0uUmVmbGVjdGlvbjsKCQkJCQkKcHVibGljIGNsYXNzIFByb2dyYW0gewoJcHVibGljIHN0YXRpYyB2b2lkIE1haW4oKSB7CgkJdmFyIG9iamV0byA9IG5ldyBUaXBvKCkgeyB4ID0gMSwgYSA9ICJhYWEiIH07CgkJdmFyIG9iamV0bzIgPSBPYmplY3RVdGlsPFRpcG8+LkNsb25lKG9iamV0byk7CgkJQ29uc29sZS5Xcml0ZUxpbmUob2JqZXRvMi54KTsKCX0KfQoKc3RhdGljIGNsYXNzIE9iamVjdFV0aWw8VD4gewoJcHVibGljIHN0YXRpYyBUIENsb25lKFQgb2JqKSB7CgkJVHlwZSB0eXBlID0gb2JqLkdldFR5cGUoKTsKCQl2YXIgZmllbGRzID0gdHlwZS5HZXRGaWVsZHMoQmluZGluZ0ZsYWdzLlB1YmxpYyB8IEJpbmRpbmdGbGFncy5Ob25QdWJsaWMgfCBCaW5kaW5nRmxhZ3MuSW5zdGFuY2UpOwoJCXZhciBjbG9uZWQgPSBBY3RpdmF0b3IuQ3JlYXRlSW5zdGFuY2UodHlwZSk7CgkJZm9yIChpbnQgaSA9IDA7IGkgPCBmaWVsZHMuTGVuZ3RoOyBpKyspIGZpZWxkc1tpXS5TZXRWYWx1ZShjbG9uZWQsIGZpZWxkc1tpXS5HZXRWYWx1ZShvYmopKTsKCQlyZXR1cm4gKFQpY2xvbmVkOwoJfQkKfQoKY2xhc3MgVGlwbyB7CglwdWJsaWMgaW50IHg7CglwdWJsaWMgc3RyaW5nIGE7CglwdWJsaWMgVGltZVpvbmVJbmZvIHQ7Cn0KCi8vaHR0cHM6Ly9wdC5zdGFja292ZXJmbG93LmNvbS9xLzE3NzEzNy8xMDE=