Access code having same name but different case from VB?

  • Thread starter Thread starter Benjamin Lukner
  • Start date Start date
B

Benjamin Lukner

Hi!

I have a C# driver dll for a barcode scanner that exposes several
controls and classes. The problem is that the controls are written in
upper case and the classes in upper and lower case,
e.g. UPCEAN & UPCEan.

It's no problem to use the dll in C#, but VB always picks the control
and I have no access to the class. I tried to edit the code with a text
editor and compile it in the dev env. without opening the file, but it
didn't help.

Does anybody know a trick how to get access to the classes under VB?
The only thing I can imagine is writing a wrapper dll with different
names for the classes and controls in C# ...


Kind regards,

Benjamin Lukner
 
The option you describe (wrapper C# dll) is the only one that comes to mind
if you don't want/can't tamper with the existing one.

I suggest, next time you use a 3rd party C# dll ask them to mark it (e.g. in
the AssemblyInfo.cs) with:
[assembly: System.CLSCompliant(true)] //done by default in VB

Cheers
Daniel
 
.... actually I should say that reflection would also work in this case but
unless you only need a couple of calls or you don't care about perf I would
go for the wrapper.

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


Daniel Moth said:
The option you describe (wrapper C# dll) is the only one that comes to
mind if you don't want/can't tamper with the existing one.

I suggest, next time you use a 3rd party C# dll ask them to mark it (e.g.
in the AssemblyInfo.cs) with:
[assembly: System.CLSCompliant(true)] //done by default in VB

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


Benjamin Lukner said:
Hi!

I have a C# driver dll for a barcode scanner that exposes several
controls and classes. The problem is that the controls are written in
upper case and the classes in upper and lower case,
e.g. UPCEAN & UPCEan.

It's no problem to use the dll in C#, but VB always picks the control and
I have no access to the class. I tried to edit the code with a text
editor and compile it in the dev env. without opening the file, but it
didn't help.

Does anybody know a trick how to get access to the classes under VB?
The only thing I can imagine is writing a wrapper dll with different
names for the classes and controls in C# ...


Kind regards,

Benjamin Lukner
 
Daniel said:
.... actually I should say that reflection would also work in this case but
unless you only need a couple of calls or you don't care about perf I would
go for the wrapper.

Thanks for your fast reply!

Do you have some sample code by chance showing how to use reflection?
I tried to do, but I can only load the dll and show its file name ;)


Kind regards,

Benjamin Lukner
 
You don't need to load the dll via reflection. Reference and use it as you
normally would. Use reflection only when you need to get past the case
sensitivity issue. E.g.

//'C# Server code
namespace Test{
public class SomeClass{
public void SomeMethod(){
System.Windows.Forms.MessageBox.Show("1","ine");
}
public void somemethod(){
System.Windows.Forms.MessageBox.Show("2","two");
}
}
}

'//VB Client code in some method
Dim o As New Test.SomeClass
Dim t As Type = o.GetType()
Dim mi As MethodInfo
mi = t.GetMethod("somemethod")
mi.Invoke(o, Nothing)
mi = t.GetMethod("SomeMethod")
mi.Invoke(o, Nothing)

Cheers
Daniel
 
Hi!

I tried, but still have a problem...
The naming collision is between a control and a class.
I now can access the class, but I don't know how to access its properties:

Dim oSettings As xxx = New xxx

Dim t1 As Type = oSettings.GetType
Dim t As Type
Dim mis() As System.Reflection.MemberInfo
Dim mi As System.Reflection.MemberInfo
Dim pi As System.Reflection.PropertyInfo

mis = t1.GetMember("UPCEan")
mi = mis(0) ' Now I have access to my class

t = mi.GetType ' !! This is the code line that does not work
' !! because I get System.MemberInfo, of course

pi = t.GetProperty("EnableUPCA") ' Returns Nothing
pi.SetValue(pi, True, Nothing)


Is it possible to convert the MemberInfo to its original class or get
access to its members anyway?


Kind regards,

Benjamin Lukner
 
Sorry, not sure I understand the question. On properties, working with my
original example the following should work:

//'C# side
public string SomeProp{get{return "Hi";}}

'// VB side
Dim pi As PropertyInfo = t.GetProperty("SomeProp")
MessageBox.Show(pi.GetValue(o, Nothing))

Now, I feel I must reiterate my suggestion to go down the wrapper route.
Just a bunch of thin C# proxy objects will be easier to code against and
faster at runtime and easier to maintain for the future.

If you insist on the reflection path, I suggest you code against the library
as you normally would (not all of it has to compile) and then translate
that code using reflection one step at a time.

Cheers
Daniel
 
You don't want to use MemberInfo. You need the either MethodInfo or
PropertyInfo

Dim pi as PropertyInfo = oSettings.GetType().GetProperty("UPCEan")
Dim prop as Object = pi.GetValue(oSettings)
pi = prop.GetType().GetProperty("EnableUPCA")
Dim val as Boolean = pi.GetValue(prop, Nothing)
pi.SetValue(prop, Nothing, True)
 
Back
Top