简体   繁体   中英

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.Controls[0]);
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.

Try this:

dynamic playButton = FooInstance.playButton;

playButton.Image = (Image)pauseBitmap;

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