jeudi 21 mai 2015

How To Change Property Value Which Was Get By Reflection C#

I have a dll which has a button class. it looks something like that:

internal class CoolButton12345: UserControl
{
  all fields, properties and methods...
  public Image Image{get;set;}
}

I'm trying to change this button property value to my own.

dynamic playButton = typeof (FooDll).Assembly.GetType("ClassWhichHasThisButton")
                .GetFields(BindingFlags.Instance | BindingFlags.NonPublic)
                .FirstOrDefault(x => x.Name == "playButton").GetValue(FooInstance);
playButton.Image = (Image)pauseBitmap;

But I'm getting this error

An unhandled exception of type 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' occurred in System.Core.dll

Why my method is wrong? Please tell me can I do something like that or not? If I can please help to find the way how to do that.





Aucun commentaire:

Enregistrer un commentaire