Emit Reflection(vb.net)动态反映dropbox结果属性网格 [英] Emit Reflection (vb.net) Dynamically Reflect dropbox results in property grid

查看:88
本文介绍了Emit Reflection(vb.net)动态反映dropbox结果属性网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用一个具有下拉列表的属性(自定义类型)向属性网格添加属性



我正在使用VS2010 VB.net反思



我的完整解决方案 - 在这里下载



https://www.nyvault.com/files/reflection/xml_propgrid_reflect_sk.zip



密码是1



这个项目的重点是填充一个propertyGrid我不想创建自定义网格,我只想填充已经制作的盒子通用微软的东西。



基本上,我得到了用反射创建的类。使用XML中的数据有些字段使用自定义类型(这将用于下拉列表)



这是我运行解决方案时发生的事情:所以我做这个类,它看起来很好它创建了一切并设置它。然后当我在我的MAIN()中创建这个类的实例时,调用类型的默认构造函数[new()](这是硬编码的下拉项)而不是我想要的自定义构造函数[new(byval test as integer)]



基本上这里是自定义类型的类构造函数(位于 customlist.vb



trying to add properties to a property grid with one property having a dropdown(custom type)

Im using VS2010 VB.net with reflection

For my full solution - Download it here

https://www.nyvault.com/files/reflection/xml_propgrid_reflect_sk.zip

Password is 1

The point of this project is to populate a propertyGrid I dont want to create a custom grid, i just want to populate the already made out of the box generic microsoft thing.

basically, I get the class to be created with reflection. using data from XML Some of the fields use a custom type (this will be used for the dropdown)

This is what happens when I run the solution: So i make the class, and it looks fine It creates everything and sets it up. then when I make an instance of this class in my MAIN() in calls a default constructor [new()] for the type(which is hardcoded dropdown items) instead of the custom constructor I wanted [new(byval test as integer)]

basically here are the class constructors for the custom type (located in customlist.vb)

Public Sub New()     
        '' Gather all the localized strings currently loaded
        '' Gather all the strintTables from the current project.    
        For i As Integer = 0 To 4
            myStringCollection.Add(New MyString(100 + i, "Test " & i))
        Next
    End Sub


   Public Sub New(ByVal val As Integer)      
        For i As Integer = 0 To val
            myStringCollection.Add(New MyString(100 + i, "Testy " & i))
        Next
    End Sub





它调用 Public Sub新的() 但我想调用 Public Sub New(ByVal val As Integer) < br $> b $ b

请帮忙,这让我把头发拉了两个月。



it calls the Public Sub New() but i want to call the Public Sub New(ByVal val As Integer)

please help, this has been making me rip my hair out for two months.

推荐答案

你的MyStringTypeConverter是类的TypeConverter属性。你不能将自定义构造函数添加到类型转换器中,并期望框架调用它们知道传入val参数的内容。幸运的是,.NET框架可以解决这个问题...



首先,你可以创建一个新的属性,称之为MyStringValueAttribute,并添加一个属性这个属性叫做Value。将它添加到您使用MyStringTypeConverter的类中。



当TypeConverter调用CanConvertFrom或CanConvertTo时,它会传递一些上下文信息。你可以使用反射来查看你的属性是否附加到类中,如果是,你可以加载值并以这种方式创建你的字符串表。



我能想到的另一件事就是将Val属性添加到MyString中。它真的不清楚类型转换器中的val是什么,或者你想要在那里得到什么,所以我不能告诉你最好放的地方。



在构造函数中没有其他任何方法可以做到这一点,框架不会调用它。



也许如果你提供了一些关于你想做什么的更多信息而不是你想要做什么,我们可以帮助你得到更好的答案。我只能说你是从XML文件创建一个类,但我真的不知道MyString在哪里播放,以及自定义构造函数应该做什么......
Your MyStringTypeConverter is a TypeConverter attribute to a class. You can''t add custom constructors to a type converter and expect the framework to call them knowing what to pass in for the "val" parameter. Luckily the .NET framework has a way around this...

First, you could create a new Attribute, call it something like MyStringValueAttribute, and add a single property to this attribute called Value. Add it to your class that you use the MyStringTypeConverter on.

When the TypeConverter calls CanConvertFrom or CanConvertTo, it passes some context information. You can use reflection to see if your attribute is attached to the class and if it is, you can load the value in and create your string table that way.

The only other thing I can think of is to add the Val property to the MyString. It really isn''t clear what the "val" is for in the type converter or what you are trying to get at there, so I can''t tell you the best place to put it.

There really isn''t any other way to do this in the constructor, the framework won''t call it.

Maybe if you gave some more information about what you are trying to do rather than how you are trying to do it, we can help you get some better answers. All I can tell is you are creating a class from an XML file but I really don''t know where the MyString plays into it, and what the custom constructor is really supposed to do...


这篇关于Emit Reflection(vb.net)动态反映dropbox结果属性网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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