用VB能实现对象的属性遍历吗?
VB有没有类似于VC++一样的对象序列化操作?

解决方案 »

  1.   

    用type info可以遍历控件的属性
    VB没有Serialize,手工代码导出对象吧
      

  2.   

    '工程引用Accessing Type Libraries from VB (System32\tlbinf32.dll):
    http://www.wd-mag.com/articles/1999/9912/9912b/9912b.htm?topic=articles
      

  3.   

    Accessing Type Libraries from VB
    William R. Epp--------------------------------------------------------------------------------Type libraries provide meta information about libraries: the names of functions, their parameters, and so on. Microsoft provides low-level interfaces that let programs browse and even create type libraries. There are a number of uses for this ability. For example, you might want to create a custom user interface for browsing or maintaining type libraries. In another example, a program that can be extended with custom COM objects might want to programmatically inspect the type library of a DLL before calling it directly, just to ensure that it conforms to certain specifications. C++ is typically used to access the low-level interfaces Microsoft provides for type libraries, but this article will demonstrate that you can get at type libraries directly from VB without using C++ at all.VB ships with tlbinf32.dll, which gives VB programs an interface for accessing type libraries. Though this file is undocumented and unsupported by Microsoft, you can redistribute it with your own projects (see license.txt and redist.txt in the VB5 or VB6 folder). See Knowledge Base article Q172988 for the official word. (Point your browser at http://support.microsoft.com/support/kb/articles/Q172/9/88.asp or email [email protected] with a subject line of “Q172988”.) The only other references to this file in the MSDN library are in the books section under “Advanced Microsoft Visual Basic 5” in chapter 8 and “Hardcore Visual Basic” in chapter 10, and that information is limited.A Sample Program
    To demonstrate using type library information from Visual Basic, I first wrote a simple COM server (comservr.vbp in this month’s code archive). This COM server exposes three properties: Interest is a Double that specifies an interest rate (e.g., 0.095 for 9.5%); Term is a Long that contains the length of a loan in months; and Amount is a Currency variable that contains the dollar amount of the loan. The server also exposes three methods:Function calculatePayment1 () As Currency
    Function calculatePayment2 (dInterest As Double, _
         LTerm As Long, _
         CAmount As Currency) As Currency
    Function calculatePayment3 (Optional dInterest As Double, _
         Optional lTerm As Long,  _
         Optional cAmount As Currency) As CurrencyAll the methods return the monthly payment amount.This simple COM server provides some dummy code whose type library information you can inspect. Run comservr.exe once to register the program for use. Don’t forget to typeCOMServr /unregserverbefore deleting the .exe, to ensure that the associated registry entries get removed.The second program (in project typeinfo.vbp in this month’s code archive) uses the simplified type library interface that tlbinf32.dll provides to let you display type library information. The main source code is in typeinfo.bas (Listing 1), and some less important functions are in typemisc.bas in this month’s code archive. You can execute typeinfo.exe and then either enter the COM name (e.g., COMServr.clsLoanPayment) or select the file name (e.g., comservr.exe) of the object/file containing the type library information you want to display. Note that you may enter the COM name only for objects Visual Basic can dynamically load using CreateObject(); typically OCXs will not work here. You may still get type library information from an OCX file (or any file with a type library) using TypeLibInfoFromFile, you just won’t be able to invoke any of its methods. For an example of an OCX that does work, try MSComDlg.CommonDialog or comdlg32.ocx.Once you load the object, the program displays the method names and return types (functions and property get/puts) in a treeview control. To reduce clutter, the program avoids displaying standard COM methods such as QueryInterface() and AddRef(). typeinfo.exe gets this information from the MemberInfo property by looping through the Members collection of the InterfaceInfo property. The member ID is saved in the treeview nodes’ Tag property, so it is readily available should the user invoke the method. The dash is important in the treeview node’s Text property as the program uses it to differentiate between methods and parameters. If a method has any parameters, their name and type are displayed in treeview child nodes of the method node. This information is retrieved from the Parameters collection of the MemberInfo property. The parameter type is saved in the treeview nodes’ Tag property so it is handy for the invoke method.To test the calculatePayment1 method, double-click the “Interest - Property Put” item and enter a value. Do the same for the Term and Amount properties and then double click the caclulatePayment1 method to get the result. To verify properties are set just double-click the properties’ “Property Get” item and the current value will be displayed. For methods with parameters like calculatePayment2, you are prompted (sorry, I used InputBox to keep the program size down) for the parameter(s) and then the method is invoked and the return value displayed.The calculatePayment3 method demonstrates how to handle optional parameters — if you don’t enter anything when prompted for a value, typeinfo.exe assumes this is an optional parameter and removes the array entry that will be passed to InvokeHookArray. Be aware this means that if you aren’t passing the second parameter, you may not pass the third. Also note that this array is zero-based and InvokeHookArray expects the parameters in reverse order. A property put uses the InvokeHook method as there is only one parameter, as does the property get which has no parameters. The InvokeHook method requires that you specify the parameters in the same order as the method expects them and is not as useful for dynamic method invocation.More Information
    If you’ve never dealt with type library information before, be prepared for confusion. The tlbinf32.dll object model does hide a lot of the messy details, but you’ll notice the same information is available in one or more different ways — which models the underlying COM type library interfaces. Hopefully Microsoft will decide to document this useful object more completely in the near future. For further reading on type library information, see Inside OLE by Craig Brockschmidt, especially chapter three which discusses the ITypeLib, ITypeInfo, and IProvideClassInfo COM interfaces. If you have MSDN, see the Interfaces section under Platform, SDK and DDK Documentation | Object Services | COM | Reference.Microsoft’s home page for COM information is currently http://www.microsoft.com/com/. If you look there for specifications under the Resources heading, you can find a page that lets you download a copy of the COM specifications. Unfortunately it does not contain the type library information section! The information missing there is currently available as part of the Win32 Platform SDK, however. Go to http://msdn.microsoft.com, click on “Products”, then click on “Microsoft Platform SDK”. If you have Win98 or NT 4, you should be able to download the latest version of the SDK (though you may have to first download an updated version of Windows Installer). Unless you have a very fast network connection, you will probably want to download just a subset of the SDK. The documentation for COM is the portion that has the type library information. Once you download that, you should end up with a file named something like Platform SDK/help/automat.chm, which is the HTML Help file that documents OLE automation, which includes the type library interface documentation.William R. Epp is an IM Consultant for Neutrogena, a proud member of the Johnson & Johnson family of companies (i.e., that Uncle no one talks about). Feel free to send praise, cash, or glowing comments to [email protected].
      

  4.   

    to  winehero(编程人生) 以上是可以得到控件的相关属性,假如这个属性是个对象怎么办,我能否把它的值保存到字符串然后还原呢?
      

  5.   

    我怎么样在程序运行中把控件的属性保存到类似.frm格式的文件中?