Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?
} 
like image 900
afuzzyllama Avatar asked Aug 11 '26 01:08

afuzzyllama


1 Answers

Use Array.Clone():

if(property.PropertyType == typeof(System.Byte[]))
{
    property.SetValue(this, someArray.Clone(), null); 
} 
like image 108
D Stanley Avatar answered Aug 13 '26 14:08

D Stanley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!