通过反射获取表单文本 [英] Get Form Text through reflection

查看:76
本文介绍了通过反射获取表单文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我在我的项目中创建了多个表单。我必须将表单名称存储到数据库。我可以得到这样的类型



Hi
i've created multiple forms in my project. i have to store the form names to the databse. I can get the types like this

SampleAssembly = [Assembly].LoadFrom(Assembly.GetExecutingAssembly().Location)
       Dim types As Type() = SampleAssembly.GetTypes()





我可以使用此类型(ⅰ)请将.Name 。但我无法检索其他属性,如表单Text,Enabled,BackColor等。

这是我的代码 strVal = types(i).GetProperty(Text)。GetValue(SampleAssembly ,没什么)。但它不起作用。如何获取文本





这是我的完整代码





I can get the form name using this types(i).Name. But i could not retrive the other properties like form Text,Enabled,BackColor etc..
This is my code strVal=types(i).GetProperty("Text").GetValue(SampleAssembly, Nothing). But its not working. How to get the text


This is my Full Code

Dim SampleAssembly As Assembly
       SampleAssembly = [Assembly].LoadFrom(Assembly.GetExecutingAssembly().Location)
       Dim types As Type() = SampleAssembly.GetTypes()
        DataGridView1.Rows.Add()
       DataGridView1.Item(0, 0).Value = "Select All"
        For i As Integer = 0 To types.Length - 1
           If types(i).BaseType.Name = "Form" And types(i).Name.StartsWith("frm") And types(i).Name <> "frmPOSMaster" Then
               DataGridView1.Rows.Add()
               DataGridView1.Item(0, DataGridView1.Rows.Count - 1).Value = types(i).Name
               DataGridView1.Item(2, DataGridView1.Rows.Count - 1).Value = types(i).GetProperty("Text").GetValue(SampleAssembly, Nothing)


           End If
       Next

推荐答案

问问自己,什么是表单,类型或类型(对象)的实例?当您使用 types(i).Name 时,您会找到该类型的名称。 Text Enabled 等属性是实例属性,它们的值取决于在实例上并且对于不同的实例具有不同的值。



如果您获得了属性信息,则可以获取静态属性的值而无需引用该实例,但是对于实例(非静态)属性,您需要一个实例。以下是如何获得价值:

http://msdn.microsoft.com/en-US/library/windowsphone/develop/hh194385%28v=vs.105%29.aspx [ ^ ]。



此调用的参数是对实例的引用。对于静态属性,它应该为null。



-SA
Ask yourself, what is "Form", type or an instance of the type (object)? When you use types(i).Name, you find the name of the type. The properties like Text, Enabled, etc., are instance properties, their values depends on instance and have different values for different instances.

If you got property info, you can get the value of the static property without having a reference to the instance, but for an instance (non-static) property, you need an instance. Here is how you can get the value:
http://msdn.microsoft.com/en-US/library/windowsphone/develop/hh194385%28v=vs.105%29.aspx[^].

The parameter of this call is the reference to an instance. For a static property, it should be null.

—SA


这篇关于通过反射获取表单文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆