问题
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