自定义组件的后期绑定的问题!

machaliha 2006-05-22 10:07:59
问题的描述是这样的,
程序中包含一个 tabcontrol ,每个Tabpage实现相应的功能。
在tabcontrol的初始化过程中,从数据库中取得需要产生的TAB数量
然后 每个Tabpage 取得对应的一个自定义组件的名称
动态创建这个组件,放到Tabpage中。

问题出现了
ORG_EDIT_MAIN.ORG_EDIT_MAIN(AdoCon.ConnectionString) 是一个自定义组件
如果这样写是没问题的
dim p as ORG_EDIT_MAIN.ORG_EDIT_MAIN(AdoCon.ConnectionString)

但是事先我并不知道 自定义类的名称
可是不能 写成 dim p as var (var 是一个字符串变量,内容"ORG_EDIT_MAIN.ORG_EDIT_MAIN(AdoCon.ConnectionString)"

as 后面不能跟 字符串.

其它方法我也实验过 但是逃不出 as 后面写实际的 类型名称的噩运!
哪一位能够提供一些帮助和思路呢?
谢谢!
...全文
145 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
水如烟 2006-05-22
  • 打赏
  • 举报
回复
用接口或MustInherit类

比如我们用到的SqlConnection,
在2005是用实现System.Data.Common.DbConnection来做的;
在2003,却是用实现System.Data.IDbConnection来做.
machaliha 2006-05-22
  • 打赏
  • 举报
回复
malingxian(马领先)
你好
谢谢你的回答
由于刚开始接触 .net 还是有很多不清楚的地方
我复制了您的代码
但是 遇到一些问题
在 p_oAssembly = [Assembly].LoadFrom(AssemblyFileName)
这个位置出现了问题

Could not load file or assembly 'file:///D:\work\documents\Visual Studio 2005\Projects\MIS2005\MIS2005\bin\Debug\ORG_EDIT_MAIN' or one of its dependencies. 系统找不到指定的文件。

但是 我已经把 ORG_EDIT_MAIN 的输出目录指向这里 并且重新 rebuilder 了
为何还出现这样的问题?

谢谢
malingxian 2006-05-22
  • 打赏
  • 举报
回复
不好意思,上面的用法不是“CodeDOM”,我打错了,应该是“反射”。另外用"CodeDOM"的方式也可以达到同样的目的,而且更灵活,但是速度上要慢一些。
malingxian 2006-05-22
  • 打赏
  • 举报
回复
然后用下列代码获取TYPE并定义新类,并执行即可:
Dim cProg As cProgram
cProg = New cProgram("类库名")

If cProg Is Nothing Then
Return
Else
dim TmpType as type = cProg.GetProgType("类名称")
Dim TmpClass As Object = TmpType.Assembly.CreateInstance(TmpType.Assembly.GetName.Name & "." & TmpType.Name)
End If
malingxian 2006-05-22
  • 打赏
  • 举报
回复
我采用的是CodeDOM的方式,用于菜单的执行,可能对你有点用处:
1、建立下面这样一个类:
Imports System.Reflection
Imports System.CodeDom
Imports System.CodeDom.Compiler
Imports Microsoft.CSharp
Imports System.Windows.Forms

Public Class cProgram
#Region " 变量 "
Private p_oAssembly As [Assembly] = [Assembly].GetExecutingAssembly
#End Region

#Region " 属性 "
Public Property [Assembly]() As [Assembly]
Get
Return p_oAssembly
End Get
Set(ByVal Value As [Assembly])
p_oAssembly = Value
End Set
End Property
#End Region

#Region " 私有方法 "

#End Region

#Region " 公共方法 "

Friend Function GetProgType(ByVal typeName As String) As Type
Try
If p_oAssembly Is Nothing Then Return Nothing
Return p_oAssembly.GetType(p_oAssembly.GetName.Name & "." & typeName)
Catch ex As Exception
Throw ex
Return Nothing
End Try
End Function

Friend Function GetProgTypes() As Type()
Try
If p_oAssembly Is Nothing Then Return Nothing
Return p_oAssembly.GetTypes
Catch ex As Exception
Throw ex
Return Nothing
End Try
End Function

Friend Function GetProgModule(ByVal ModuleName As String) As [Module]
Try
If p_oAssembly Is Nothing Then Return Nothing
Return p_oAssembly.GetModule(p_oAssembly.GetName.FullName & "." & ModuleName)
Catch ex As Exception
Throw ex
Return Nothing
End Try
End Function

Friend Function GetProgModules() As [Module]()
Try
If p_oAssembly Is Nothing Then Return Nothing
Return p_oAssembly.GetModules
Catch ex As Exception
Throw ex
Return Nothing
End Try
End Function

Friend Overloads Function GetMethod(ByVal TypeorModuleName As String, ByVal MethodName As String) As MethodInfo
Try
If p_oAssembly Is Nothing Then Return Nothing
Dim oType As Type = p_oAssembly.GetType(p_oAssembly.GetName.Name & "." & TypeorModuleName)
If Not oType Is Nothing Then
Return oType.GetMethod(MethodName)
Else
Dim oModule As [Module] = p_oAssembly.GetModule(p_oAssembly.GetName.FullName & "." & TypeorModuleName)
Return oModule.GetMethod(MethodName)
End If
Catch ex As Exception
Throw ex
End Try
End Function

Friend Overloads Function GetMethod(ByVal SourceType As Type, ByVal MethodName As String) As MethodInfo
Try
If p_oAssembly Is Nothing Then Return Nothing
Dim oType As Type = SourceType
Return oType.GetMethod(MethodName)
Catch ex As Exception
Throw ex
End Try
End Function

Friend Overloads Function GetMethod(ByVal SourceModule As [Module], ByVal MethodName As String) As MethodInfo
Try
If p_oAssembly Is Nothing Then Return Nothing
Dim oModule As [Module] = SourceModule
Return oModule.GetMethod(MethodName)
Catch ex As Exception
Throw ex
End Try
End Function

#End Region

#Region " 构造函数 "
Public Sub New(Optional ByVal AssemblyFileName As String = "")
If AssemblyFileName.Trim <> "" Then
p_oAssembly = [Assembly].LoadFrom(AssemblyFileName)
End If
End Sub

Public Sub New(ByVal [assembly] As [Assembly])
p_oAssembly = [assembly]
End Sub
#End Region

End Class
zhoujijunnt 2006-05-22
  • 打赏
  • 举报
回复
这个问题比较搞
malingxian 2006-05-22
  • 打赏
  • 举报
回复
To machaliha(machaliha):
我在运行中没发现错误啊,不过你的是.NET 2005,不知会不会有此类问题,看了你的回帖,我特意用p_oAssembly = [Assembly].LoadFrom("E:\Programs\HSGPS\GPSManager\cBaseForm.dll")测试了一下,没发现错误,也许楼上的老兄说得有道理。

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧