Roslyn: Compile time constant some reflection values

Created on 29 Apr 2016  路  12Comments  路  Source: dotnet/roslyn

For example typeof(MyClass).Namespace could be an interned string?

0 - Backlog Area-Language Design Feature Request

Most helpful comment

@benaadams

fullnameof()

馃槃

All 12 comments

Duplicate of dotnet/coreclr#2591 (though it doesn't explicitly mention Namespace or other string properties). Not sure whether this would be better implemented in Roslyn or CLR.

@benaadams Do you really mean "compile-time constant" in the language sense (i.e. could be using in a switch case expression), or do you mean you want more efficient generated code?

@gafter literally a compile time constant for the string values in the style of nameof(); where appropriate - not everything could go this way but some things could be resolved at compile time rather than runtime or jit time.

So typeof(MyClass).FullName could be resolved at compile time, but Type t; ... t.FullName probably couldn't.

More complex reflected items like bindings or assembly name I don't think would be able to be resolved at compile time as they may change as dlls were swapped or updated.

A corner case would be typeof(MyClass).FullName would fail quite badly at runtime if MyClass has changed namespace in or been removed form a dependent dll. However it would return a value if it was a compile time constant. However that behaviour is similar to nameof.

So this is actually a language change request, as whether or not a string is a constant is visible to the language.

Not sure; currently they are evaluated at runtime? Common use cases are in diagnostics and exceptions+logging which are slow areas anyway. Not hugely fussed about them working in switch would just be a nice extrapolation.

Or could be an extension to nameof to add namespace qualification to stay away from Type?

@benaadams

namespaceof()

@paulomorgado that would work.

Rather than going

nameof(System) + '.' nameof(Collections) + '.' nameof(Generic) or typeof(Dictionary<string,string>).Namespace

You could go

namespaceof(Dictionary<string,string>)

or
namespaceof(Dictionary<string,string>) + '.' + nameof(Dictionary<string,string>)

Rather than typeof(Dictionary<string,string>).FullName

@benaadams

fullnameof()

馃槃

Basically, most of properties coming out of typeof(and the type itself #8194) can be treated as compile-time constants (if the compiler cares enough). I think the "syntax" in the original post is the most flexible way of doing this, typeof(T).FullName. So practically typeof(T).Name and nameof(T) would be the same. (where T is not a generic type).

What I really want to see is the ability to do some computation on these values (read pure functions) and they still remain compile-time constants.

@benaadams May you move this to csharplang repo?

I like it, and I am working on a use case where that might be interesting. i.e.

```C#
namespace Foo
{
class Bar { }
}

// ...

const string barNameSpace = namespaceof(Bar); // "Foo" const string
```

Was this page helpful?
0 / 5 - 0 ratings