从基类继承类中设置字段的值

Вла*_*р В -2 c#

我认为这很容易,但我不知道如何:)

class base1
{
    public int x = 1;
//... many other fields
}

class inherit1:base1
{
    int y = 5;
  ...
}
base bs=new base1();
// set many fields value of the bs class
bs.value1=5;
bs.value15="sss";
//....set other fileds values

inherit1 i1=new inherit1(); 
Run Code Online (Sandbox Code Playgroud)

将所有字段值继承的类 i1 设置为等于基本字段值 bs 的最快方法是什么?
我想做这样的事情:

i1=bs;
Run Code Online (Sandbox Code Playgroud)

并在初始化所有其他字段 i1 之后。

谢谢!

Far*_*yev 6

您不能只将基类实例分配给派生类型。你需要转换它。

因此,您的首选是使用AutoMapper. 它可以帮助您将数据从对象类型复制到其他类型。它将自动映射具有相同名称的属性。最后,您将使用这样的代码 fe:

var derived = Mapper.Map<Base, Derived>(b);
Run Code Online (Sandbox Code Playgroud)


第二种选择是编写一个方法并使用反射。

并在构造函数中使用它:

public class Derived : Base
{
    public Derived(Base b)  
    {
        SetProperties(b);
    }

    private void SetProperties(object mainClassInstance)
    {
        var bindingFlags = BindingFlags.Public | BindingFlags.Instance;
        var mainClassType = mainClassInstance.GetType();

        MemberInfo[] members = mainClassType.GetFields(bindingFlags).Cast<MemberInfo>()
            .Concat(mainClassType.GetProperties(bindingFlags)).ToArray();

        foreach (var memberInfo in members)
        {
            if (memberInfo.MemberType == MemberTypes.Property)
            {
                var propertyInfo = memberInfo as PropertyInfo;
                object value = propertyInfo.GetValue(mainClassInstance, null);

                if (null != value)
                    propertyInfo.SetValue(this, value, null);
            }
            else
            {
                var fieldInfo = memberInfo as FieldInfo;
                object value = fieldInfo.GetValue(mainClassInstance);

                if (null != value)
                    fieldInfo.SetValue(this, value);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

那么你只需要:

Base b = new Base {...};
Derived d = new Derived(b);
Run Code Online (Sandbox Code Playgroud)

此外:

实际上,将SetProperties方法作为扩展方法会更好。

public static class ObjectExtensions
{
    public static void SetProperties(this object newClassIntance, object mainClassInstance)
    {
        var bindingFlags = BindingFlags.Public | BindingFlags.Instance;
        var mainClassType = mainClassInstance.GetType();

        MemberInfo[] members = mainClassType.GetFields(bindingFlags).Cast<MemberInfo>()
            .Concat(mainClassType.GetProperties(bindingFlags)).ToArray();

        foreach (var memberInfo in members)
        {
            if (memberInfo.MemberType == MemberTypes.Property)
            {
                var propertyInfo = memberInfo as PropertyInfo;
                object value = propertyInfo.GetValue(mainClassInstance, null);

                if (null != value)
                    propertyInfo.SetValue(newClassIntance, value, null);
            }
            else
            {
                var fieldInfo = memberInfo as FieldInfo;
                object value = fieldInfo.GetValue(mainClassInstance);

                if (null != value)
                    fieldInfo.SetValue(newClassIntance, value);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,您可以将此方法用作: this.SetInheritedProperties(b);