-
Nov 30th, 2020, 08:20 PM
#1
Thread Starter
Fanatic Member
VB.NET Late Binding in C#
I had a VB6 code long time ago then I convert to VB.NET. Now I am working on a version in C#, Can I totally get rid of NewLateBinding because C# doesn't have such thing? I means not making two references of using Microsoft.VisualBasic and using Microsoft.VisualBasic.CompilerServices.
Code:
object objectValue = WebBrowser1.Document.InvokeScript("z");
while (!IsDBNull(NewLateBinding.LateGet(objectValue, null, "range", new object[0], null, null, null)))
{
//...
}
Last edited by DaveDavis; Nov 30th, 2020 at 08:26 PM.
-
Nov 30th, 2020, 09:37 PM
#2
Re: VB.NET Late Binding in C#
You should probably have fixed the VB.NET code to get rid of the late-binding first. What type is the actual object you are dealing with? Is there a reason that you can't cast as that type?
If you really must use late-binding in C# then you can use the dynamic keyword these days. It works slightly differently than in VB but achieves the same result.
-
Nov 30th, 2020, 09:39 PM
#3
Re: VB.NET Late Binding in C#
By the way, as you have asked it, this is a C# question. I won't do anything yet as it may turn out that you should be making changes in VB first anyway. If not, I will ask the mods to move this thread to the C# forum.
-
Nov 30th, 2020, 10:28 PM
#4
Thread Starter
Fanatic Member
Re: VB.NET Late Binding in C#
Originally Posted by jmcilhinney
What type is the actual object you are dealing with?
it is WebBrowser1.Document.
Code:
object objectValue = WebBrowser1.Document.InvokeScript("z");
while (!IsDBNull(NewLateBinding.LateGet(objectValue, null, "range", new object[0], null, null, null)))
{
//...
}
-
Nov 30th, 2020, 11:37 PM
#5
Re: VB.NET Late Binding in C#
Originally Posted by DaveDavis
it is WebBrowser1.Document.
That's not an answer to the question I asked. The issue here is that you are declaring objectValue as type Object and so you only have compile-time access to members of that type. The object that you are assigning to that variable is obviously a more specific type and you want to access a member - seemingly a range field or property - of that type. In order to access that member without using late-binding, you need to cast a reference to that object as that type. That tells the compiler that it can access members of that type. That is the type I am asking about. You should know what that type is. Once you do know, you can do something like this in VB:
vb.net Code:
Dim objectValue = DirectCast(WebBrowser1.Document.InvokeScript("z"), SomeType)
The variable is then the correct type so you can use early-binding to access members of that type. The equivalent in C# would be this:
csharp Code:
var objectValue = (SomeType) WebBrowser1.Document.InvokeScript("z");
Is there a reason that you cannot access that type and cast as it? There may be, in which case this is a C# question and can be moved to the C# forum to be addressed in a manner specific to C#. If there isn't, it's a VB question because you should be using early-binding in the VB code first and then the conversion to C# is a doddle.
-
Dec 1st, 2020, 12:43 AM
#6
Thread Starter
Fanatic Member
Re: VB.NET Late Binding in C#
Originally Posted by jmcilhinney
That's not an answer to the question I asked. The issue here is that you are declaring objectValue as type Object and so you only have compile-time access to members of that type. The object that you are assigning to that variable is obviously a more specific type and you want to access a member - seemingly a range field or property - of that type. In order to access that member without using late-binding, you need to cast a reference to that object as that type. That tells the compiler that it can access members of that type. That is the type I am asking about. You should know what that type is. Once you do know, you can do something like this in VB:
vb.net Code:
Dim objectValue = DirectCast(WebBrowser1.Document.InvokeScript("z"), SomeType)
The variable is then the correct type so you can use early-binding to access members of that type. The equivalent in C# would be this:
csharp Code:
var objectValue = (SomeType) WebBrowser1.Document.InvokeScript("z");
Is there a reason that you cannot access that type and cast as it? There may be, in which case this is a C# question and can be moved to the C# forum to be addressed in a manner specific to C#. If there isn't, it's a VB question because you should be using early-binding in the VB code first and then the conversion to C# is a doddle.
My VB.NET code doesn't have cast because we used Late binding:
Code:
Dim objectValue As Object = AddressOf WebBrowser1.Document.InvokeScript("z")
While Not Information.IsDBNull(NewLateBinding.LateGet(objectValue, Nothing, "range", New Object(- 1) {}, Nothing, Nothing, Nothing))
Dim singleWord As String = Conversions.ToString(NewLateBinding.LateGet(objectValue, Nothing, "word", New Object(- 1) {}, Nothing, Nothing, Nothing))
End While
Edited: forgot to mention that InvokeScript("z") function is to execute a short javascript codes and return a custom object.
Code:
function z()
{
var t = document.body.createTextRange();
var a = t.duplicate();
//...
return
{
range : a,
word: a.text,
index: y
};
}
so, for my case, how to retrieve objectValue's range/word/index without using NewLateBinding.LateGet?
Last edited by DaveDavis; Dec 1st, 2020 at 08:00 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|