首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >发出(OpCodes.Ldfld,textBox)在转换时失败

发出(OpCodes.Ldfld,textBox)在转换时失败
EN

Stack Overflow用户
提问于 2015-04-13 01:51:27
回答 1查看 417关注 0票数 0

我正在使用Reflection.Emit构建一个简单的动态方法,它在一个简单的WPF程序(MyTextBox.Text)中获取TextBox对象的文本属性值。

调用不能正确地调用这个动态方法,我发现这行“OpCodes.Ldfld,textBox”中有错误,这要归功于VisualStudio.DebuggerVisualizers。

以下是调试时ILStream的输出:

代码语言:javascript
复制
IL_0000: /* 02  |          */ ldarg.0    
IL_0001: /* 7b  | 04000002 */ ldfld      **!"Specified cast is not valid."!**
IL_0006: /* 28  | 06000003 */ call       System.String get_Text()/System.Windows.Controls.TextBox
IL_000b: /* 2a  |          */ ret  

这是密码:

代码语言:javascript
复制
namespace MyWPFTest
{
    public partial class MainWindow1 : Window
    {
        public MainWindow1()
        {
            InitializeComponent();
        }

        private void MyTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            MyTextBox.Text = "Morning";
            DynamicMethod dm = new DynamicMethod("GetTextBoxText", typeof(void), new Type[] { }, typeof(MainWindow1), false);
            ILGenerator il = dm.GetILGenerator();
            il.Emit(OpCodes.Ldarg_0);

            FieldInfo textBox = typeof(MainWindow1).GetField("MyTextBox", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance);
            if (textBox == null)
            {
                throw new InvalidOperationException("no textbox");
            }
            il.Emit(OpCodes.Ldfld, textBox);
            var textProperty = typeof(TextBox).GetProperty("Text", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance).GetGetMethod();
            if (textProperty == null)
            {
                throw new InvalidOperationException("no Text property");
            }
            il.Emit(OpCodes.Call, textProperty);
            il.Emit(OpCodes.Ret);
            TestShowVisualizer(dm);
            dm.Invoke(null, null);
        }       
    }
}

TestSHowVisulalizer()帮助显示IL流以进行调试。

有人有使用TextBox这样的WPF控件使用Reflection.Emit的经验吗?

我编写了这个代码'var a= MyTextBox.Text‘,然后使用ilsdasm获取il。它看起来如下: PresentationFrameworkSystem.Windows.Controls.TextBox init ( string a) IL_0000: nop IL_0001: ldarg.0 IL_0002: ldfld类.locals MyWPFTest.MainWindow1::MyTextBox IL_0007: callvirt实例字符串PresentationFrameworkSystem.Windows.Controls.TextBox::get_Text() IL_000c: stloc.0 IL_000d: ret } // end of method MainWindow1::MyTextBox_TextChanged

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-04-13 02:50:20

如果您阅读MSDN上的示例,您会发现默认情况下,参数列表中没有包含this参数。指定owner类的DynamicMethod使您可以访问私有成员,而不是this参数。您的DynamicMethod就像C#源代码中的一个static方法。

现在,您有一个空的参数类型数组,因此没有Ldarg_0

尝试指定参数类型。变化

代码语言:javascript
复制
DynamicMethod dm = new DynamicMethod("GetTextBoxText",
                                     typeof(void),
                                     new Type[] { },
                                     typeof(MainWindow1),
                                     false);

代码语言:javascript
复制
DynamicMethod dm = new DynamicMethod("GetTextBoxText",
                                     typeof(void),
                                     new Type[] { typeof(MainWindow1) },
                                     typeof(MainWindow1),
                                     false);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29596798

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档