简体   繁体   English

如何改变通过反思得到的财产价值C#

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

I have a dll which has a button class. 我有一个按钮类的DLL。 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 System.Core.dll中发生未处理的“Microsoft.CSharp.RuntimeBinder.RuntimeBinderException”类型异常

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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