c# - 如何使用反射复制数组?

标签 c# .net reflection

如果我有以下字节数组:

byte[] someArray = new byte { 0, 1, 2 };

我想通过反射将它复制到一个类的实例中,你怎么做到的?

// Inside a class method

PropertyInfo property = this.GetType().GetProperty("propertyName");

if(property.PropertyType == typeof(System.Byte[]))
{
    property.SetValue(this, ???, ???); // How to set an array?
} 

最佳答案

使用Array.Clone() :

if(property.PropertyType == typeof(System.Byte[]))
{
    property.SetValue(this, someArray.Clone(), null); 
} 

关于c# - 如何使用反射复制数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14168525/

相关文章:

c# - 如何使我的 asp 日历弹出?

c# - 在 C# 中,启动时,有没有办法强制 JIT 编译器接触整个应用程序中的所有 MSIL 代码?

c# - 从 GUID 获取类型

c# - 检查字典中key的值是否为null

c# - 在 Multi-Tenancy .net 项目中实现角色的最佳方式是什么?

c# - 当第二列的内容可见时,如何将 Xaml 网格列 50/50 拆分?

javascript - 复杂的 C# 对象

.net - .net Windows编译器无法正常工作

java - 反射运行时性能 - Java 与 CLR

c# - 如何在C#中使用反射获取方法的所有属性和属性数据