简体   繁体   中英

How to copy an array using reflection?

If I have the following byte array:

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

and I want to copy it to an instance of a class through reflection, how can you do that?

// Inside a class method

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

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

Use Array.Clone() :

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

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM