Why can't I get the number of censuses at compile time?

I asked How can I get the number of transfers as a constant? , and I found out that at compile time I cannot get the number of enumerations because C # uses reflection for this.

I read What is reflection and why is it useful? , so I have a basic understanding of reflection.

To get the number of listings, I can use Enum.GetNames(typeof(Item.Type)).Length; , and this happens at runtime using reflection.

I don’t see any runtime knowledge needed to get the number of transfers, because as far as I know, the number of transitions cannot be changed at runtime.

Why does C # need to use reflection to get the number of enumerations? Why can't he do this at compile time?

+7
reflection enums c # count compile-time
source share
3 answers

Just because something can be evaluated at compile time does not mean that someone has programmed a compiler for this. string.Format("{0:N2}", Math.PI) is another example.

The only way to get a counter of the number of Enum values ​​is via reflection ( Enum.GetNames or something similar). Thus, this is not a constant expression, although technically the compiler could simply evaluate the expression at compile time and determine which result.

nameof is a great example. It is persistent at compile time, but there was no mechanism for extracting the result at compile time until someone developed, developed, tested, documented, and sent this function. None of them is free, and therefore the idea should compete for valuable resources (people, time, money) from other functions that may be more valuable.

So, if you think that a compile-time construct such as enumcount(Item.Type) is a valuable addition to the language, then you can more than post the Connect clause and see if it gets to the top of the list of functions.

But I need this number as a constant number, so I can use it in the Unity function [Range (int, int)].

One imperfect workaround is to define a constant that matches the current number of enumeration elements and throw an exception at runtime if the counters do not match:

Define a public constant next to your listing by commenting on it so that developers know to update it:

 // Update this value any time the Type enum is updated public const int TypeCount = 5; public Enum Type { Bar1, Bar2, Bar3, Bar4, Bar5, } 

use it in your attribute:

 [Range(0, Item.TypeCount)] public void BlahBlahBlah() {} 

and check it at the beginning of your application:

 public static Main() { if(Enum.GetNames(typeof(Item.Type)).Length != Item.TypeCount) throw new ApplicationException ("TypeCount and number of Types do not match.\nPlease update TypeCount constant.") } 
+4
source share

I think in simple words:

Enums is one type definition, .NET uses Reflection when a type descriptor is required.

So Enums is a type and @runtime, if you want to count a specific enums vote, you need a user reflection.

0
source share

I do not see any runtime knowledge needed to get the number of enumerations, because as far as I know, the number of enumerations cannot be changed at runtime.

Here is the error in your reasoning: Yes, the number of transitions cannot be changed at runtime. However, it can be changed between runtimes:

 A.dll - version 1 public enum Foo { A } A.dll - version 2 public enum Foo { Bar, Baz } 

Replace version 1. of the A.dll file with version 2. The number of enumerations has changed (and the names of the values ​​as well).

Why does C # need to use reflection to get the number of enumerations? Why can't this be done at compile time?

He can do it. But then you ran into the problem above. The calculated compile time value may become incorrect.

0
source share

All Articles