Allow using 'typeof' keyword with definitely typed variable/property as a parameter #2847
Replies: 8 comments
-
This is largely a duplicate of #988. |
Beta Was this translation helpful? Give feedback.
-
Edit: debunked below, thanks! |
Beta Was this translation helpful? Give feedback.
-
Need I continue? |
Beta Was this translation helpful? Give feedback.
-
This is not true as of .Net Core 2.2 Not that it changes the thrust of your argument. |
Beta Was this translation helpful? Give feedback.
-
That shouldn't be the case. Indeed, the JIT will simply replace the entire operation with the actual type in-situ. There will no actual cost beyond that when the code executes. |
Beta Was this translation helpful? Give feedback.
-
Not on 32-bit .NET Framework. 😢 But yes, 64-bit .NET Framework and .NET Core are smarter about it. |
Beta Was this translation helpful? Give feedback.
-
Ah, I didn't know this stuff. Thanks! |
Beta Was this translation helpful? Give feedback.
-
using VariableType = Foo.Bar;
var type = typeof(VariableType); |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Let's assume, you have some POCO class with few properties:
And you need to generate and store some metadata for this class.
You can write:
But you can't use:
Although compiler has all information about Foo type (unless Foo is dynamic).
You need to write instead:
And this will be definitely broken during refactoring, if you will change the type of Foo in POCO.
Moreover, in this situation, 'nameof' and 'typeof' implementation seems to have some inconsistency, as for me.
So my suggestion is to implemement and allow using of
At least in case when variable type can be obtained during compilation.
This issue has been moved from https://developercommunity.visualstudio.com/content/idea/389549/allow-using-typeof-keyword-with-definitely-typed-v.html
VSTS ticketId: 733912
These are the original issue comments:
Jane Wu [MSFT] on 11/23/2018, 00:14 AM (12 days ago):
Thank you for taking the time to provide your suggestion. We will do some preliminary checks to make sure we can proceed further. You will hear from us in about a week on our next steps.
Vladyslav Prokopenko on 11/23/2018, 02:27 AM (12 days ago):
Looks reasonable and natural.
As alternative solution I can propose to use GetType.
Anton Zhaparov on 11/23/2018, 02:58 AM (12 days ago):
GetType is an instance method, so it works only with class instance, not with type definition itself.
Thus to utilize it, I will need to create a dummy instance of the POCO class... Which is possible, but complicates the code, while more straightforward and elegant solution can be implemented, I believe.
Vladyslav Prokopenko on 11/23/2018, 03:05 AM (12 days ago):
Sure, GetType is not ideal solution, but for POCO's it'll work in most cases, as the class is trivially-constructible.
For other cases only reflection will help you, unfortunately.
Beta Was this translation helpful? Give feedback.
All reactions