I found the following code snippet which puzzled me.
public class Bclass : Aclass
{
public const BindingFlags Flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
public Bclass(IAclass a) : base(string.Empty)
{
var destFields = this.GetType().BaseType.GetFields( Flags );
a.GetType().GetFields( Flags ).Where(x => destFields.Contains(x)).ToList().ForEach(property =>
{
destFields.First(x => x == property).SetValue(this, property.GetValue(a));
});
var destProperties = this.GetType().BaseType.GetProperties( Flags );
a.GetType().GetProperties( Flags ).Where(x => destProperties.Contains(x)).ToList().ForEach(property =>
{
destProperties.First(x => x == property).SetValue(this, property.GetValue(a, null));
});
}
// some more methods...
}
My main Q is.... why would anyone think of doing that... What benefit(s) can come out of this code.
What it does: memberwise clone from a
into the current newly created instance
Advantages:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.