Is there a way cast an object back to it original type without specifing every case?

前端 未结 6 1376
眼角桃花
眼角桃花 2021-01-15 14:28

I have an array of different type objects and I use a BinaryWriter to convert each item to its binary equivalent so I can send the structure over the network.

I curr

6条回答
  •  梦毁少年i
    2021-01-15 15:07

    What you're asking for is Dynamic Dispatch, and C# 3.0 doesn't have it.

    You should at least use a runtime check to verify that you aren't missing a type.

    You may be able to do something clever where you have a Dictionary that maps from types to processing functions. You can fill in the mapping for all processing functions in one place. You have a better chance of getting this right than if you write a switch wherever the processing happens.

提交回复
热议问题