如何在C#解决方案中使用参数调用visual basic property getter [英] How call visual basic property getter with arguments in C# solutions
问题描述
大家好,
我很久以前写过一个很长的Visual Basic项目,其中一个类有多个具有多个参数的属性。
现在我应该在用C#编写的现有解决方案中实现该项目。我发现调用这些属性getter / setter的唯一方法是通过反射。
你知道另一种方式吗?
什么我试过了:
Hi all,
I've written a long time ago a long Visual Basic project, where a class has several properties with multiple arguments.
Now I should implement that project in an existing solution written in C#. The only way I found to call these property getter/setter is through reflection.
Do you know another way?
What I have tried:
VB
Public Class Manager
Public Property Message() As String
Get
Return Message(Nothing, Nothing)
End Get
Set(value as String)
Message(Nothing, Nothing) = value
End Set
End Property
Public Property Message(useSQLInfo As Boolean?) As String
Get
Return Message(useSQLInfo, Nothing)
End Get
Set(value as String)
Message(useSQLInfo, Nothing) = value
End Set
End Property
Public Property Message(useSQLInfo As Boolean?, manageRN As Boolean?) As String
Get
[...]
End Get
Set(value as String)
[...]
End Set
End Property
End Class
C#
class Foo {
Manager manager = CreateManager();
[...]
void SaveExchange([...]) {
if (manager != null) {
string managerMessage = ManagerGetMessage(manager, true, null);
[...]
}
}
private static MethodInfo mgmMethodInfo = null;
string ManagerGetMessage(Manager manager, bool? useSQLInfo, bool? manageRN) {
if (mgmMethodInfo == null)
mgmMethodInfo = manager.GetType().GetProperty("Message", typeof(string),
new Type[] { typeof(bool?), typeof(bool?) } ).GetGetMethod();
return mgmMethodInfo.Invoke(manager, new object[] { useSQLInfo, manageRN} );
}
}
如果有人知道另一种方法被广泛接受。
谢谢
Lucio
If anyone knows another method is well accepted.
Thank you
Lucio
推荐答案
如果VB代码在类库中,那么你可以参考并像使用C#类库一样使用它。
这是一个测试,以显示其工作原理:
1.将C#Console应用程序添加到解决方案中
2.将VB类库添加到解决方案
3.在C#控制台中引用VB类库app
4.使用以下代码:
VB类库
If the VB code is in a class library, then you can reference and use it like you can a C# class library.
Here is a test to show how this works:
1. Add a C# Console app to a solution
2. Add a VB class library to the solution
3. reference the VB class Library in the C# console app
4. use the following code:
VB Class Library
Public Class Class1
Public Function GetString() As String
Return "Hello from VB!"
End Function
End Class
C#控制台应用程序
C# Console app
namespace ConsoleTest
{
class Program
{
static void Main(string[] args)
{
var vbClass = new ClassLibrary1.Class1();
Console.WriteLine(vbClass.GetString());
Console.ReadKey();
}
}
}
5.现在编译并运行它会起作用。 :)
**物业测试更新
VB类库
5. Now compile and run and it will work. :)
** Property Test update
VB Class Library
Public Class Class1
Public Function GetString() As String
Return "Hello from VB!"
End Function
Private test As String
Public Property TestProperty() As String
Get
Return test
End Get
Set(ByVal value As String)
test = value
End Set
End Property
End Class
C#控制台应用
C# Console app
using System;
namespace ConsoleTest
{
class Program
{
static void Main(string[] args)
{
var vbClass = new ClassLibrary1.Class1();
// Test 1
Console.WriteLine(vbClass.GetString());
// Test 2
vbClass.TestProperty = "This is a VB property test";
Console.WriteLine(vbClass.TestProperty);
Console.ReadKey();
}
}
}
输出
Output
Hello from VB!
This is a VB property test
**更新2:使用参数访问VB属性
Intellisense有你的答案。见下文:
VB类库
** Update 2: Accessing VB Property with paramater
Intellisense had the answer for you. See below:
VB Class Library
Imports System.Runtime.CompilerServices
Public Class Class1
Public Function GetString() As String
Return "Hello from VB!"
End Function
Private test As String
Public Property TestProperty() As String
Get
Return test
End Get
Set(ByVal value As String)
test = value
End Set
End Property
Public Property Message() As String
Get
Return Message(Nothing, Nothing)
End Get
Set(value As String)
Message(Nothing, Nothing) = value
End Set
End Property
Public Property Message(useSQLInfo As Boolean?) As String
Get
Return Message(useSQLInfo, Nothing)
End Get
Set(value As String)
Message(useSQLInfo, Nothing) = value
End Set
End Property
Private flag1 As Boolean?
Private flag2 As Boolean?
Private messageText As String
Public Property Message(useSQLInfo As Boolean?, manageRN As Boolean?) As String
Get
Return messageText
End Get
Set(value As String)
messageText = value
End Set
End Property
End Class
C#控制台应用
C# Console app
using System;
namespace ConsoleTest
{
class Program
{
static void Main(string[] args)
{
var vbClass = new ClassLibrary1.Class1();
// Test 1
Console.WriteLine(vbClass.GetString());
// Test 2
vbClass.TestProperty = "This is a VB property test";
Console.WriteLine(vbClass.TestProperty);
// test 3
vbClass.set_Message(true, "test message");
Console.WriteLine(vbClass.get_Message(true));
Console.ReadKey();
}
}
}
这篇关于如何在C#解决方案中使用参数调用visual basic property getter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!