c#中的接口属性副本 [英] interface property copy in c#
问题描述
我已经和C#合作多年了,但是刚刚遇到这个问题让我感到困惑,我甚至不知道如何提出这个问题,所以,举个例子!
I've been working with C# for many years now, but just come across this issue that's stumping me, and I really don't even know how to ask the question, so, to the example!
public interface IAddress
{
string Address1 { get; set; }
string Address2 { get; set; }
string City { get; set; }
...
}
public class Home : IAddress
{
// IAddress members
}
public class Work : IAddress
{
// IAddress members
}
我的问题是,我想将IAddress属性的值从一个类复制到另一个类。这可能是在一个简单的单行语句中还是我仍然需要对每个语句进行属性到属性的分配?我真的很惊讶这个看似简单的事情让我感到困惑......如果不可能以简洁的方式进行,那么有没有人有任何捷径可以用来做这类事情?
My question is, I want to copy the value of the IAddress properties from one class to another. Is this possible in a simple one-line statement or do I still have to do a property-to-property assignment of each one? I'm actually quite surprised that this seemingly simple thing has stumped me like it has... If it's not possible in a concise way, does anyone have any shortcuts they use to do this sort of thing?
谢谢!
推荐答案
这里没有任何一个班轮。
There's no one-liner for this.
如果你做了很多,你可以研究某种形式的代码生成,也许使用T4模板和反射。
If you do it a lot, you could look into some form of code generation, perhaps using T4 templates and Reflection.
BTW COBOL对此有一个声明:移动相应的家庭工作。
BTW COBOL has a statement for this: MOVE CORRESPONDING HOME TO WORK.
这篇关于c#中的接口属性副本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!