Late Binding Magic under VB.NET converted to C#

こ雲淡風輕ζ 提交于 2020-01-03 17:43:11

问题


I should convert some code from VB to C#. Given following lines of VB work (I think only because option is not set to strict):

Dim someProp As SomeType
Try
    someProp = CType(SomeInstance, Object).SomeProp 
    ' ...

Due to late binding, this code is possible under VB. Of course, following won't work under C#:

SomeType someProp;
try
{
    someProp = ((object)SomeInstance).SomeProp;
    // ...

How could I formulate something similar under C#?

Thx for any tipps sl3dg3


回答1:


If you're using C# 4.0:

SomeType someProp;
try
{
    someProp = ((dynamic)SomeInstance).SomeProp;
    // ...


来源:https://stackoverflow.com/questions/8225328/late-binding-magic-under-vb-net-converted-to-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!