Today, I found some codes was written a year ago, that’s written for my own ORM implementations. The surprising feature is set derived class properties from base class, this feature always be used by some software architect. The simplify code (demo) as following:
public class A {
public A() {
var derivedProperties = this.GetType().GetProperties();
foreach (var prop in derivedProperties) {
var type = prop.PropertyType;
if (type.IsGenericType && type.GetGenericTypeDefinition()
== typeof(IIter<>)) {
var itemType = type.GetGenericArguments()[0];
prop.SetValue(this, Activator.CreateInstance(typeof(InternalIter<>).MakeGenericType(itemType)));
}
}
}
}
public interface IIter<T> {
T Data { get; }
}
internal class InternalIter<T> : IIter<T> {
public T Data { get; set; }
}
public class B : A {
public IIter<string> Data { get; set; }
}
[TestClass]
public class UnitTest1 {
[TestMethod]
public void TestMethod1() {
var b = new B();
var type = b.Data.GetType();
Assert.AreEqual(typeof(InternalIter<string>), type);
}
}
I'm a developer, I use C/C++, Java, C# and Swift, but I also embrace all the new stuff.
I'm working at Tencent. My family live in Hangzhou, Zhejiang, China. Maybe the difference with other programmers is I already married, and have a lovely daughter. (^<>^)
A man like me, is unique in the world of existence, no matter where you go, there belonging to my stage. Like desert gold, light can not hide forever! Especially my messy hair, melancholy eyes, sigh of stubble, handsome face, are deeply addicted whom even I.