假设我有 2 个这样的类:
public class Foo
{
[Required]
public string Name {get;set;}
}
public class Bar
{
// put here [Required] at run-time
public string Name {get;set;}
}
var foo = new Foo();
var bar = new Bar();
//copy the required from foo to bar
是否可以在运行时将属性从 foo 复制到 bar?
最佳答案
属性不附加到实例——它们附加到类型定义。
虽然您可以在运行时使用反射创建新类型,但您不能更改现有类型定义。
关于c# - 在运行时将属性的属性从一个实例复制到另一个实例(不同类型),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3037805/