TypeOf Enhancements #988
Replies: 10 comments
-
TL;DR: public class Foo {
public int Bar { get; set; }
public string Baz;
public double M() {
// existing syntax
var t1 = typeof(Foo); // type is Foo
// proposed syntax
var t2 = typeof(this); // same as typeof(Foo)
var t3 = typeof(Bar); // same as typeof(System.Int32)
var t4 = typeof(Baz); // same as typeof(System.String)
var t5 = typeof(M); // legal? typeof(System.Double)?
}
} I largely like the idea, however I have a few qualms:
|
Beta Was this translation helpful? Give feedback.
-
For members, I think I would prefer And this proposal has the same issue as |
Beta Was this translation helpful? Give feedback.
-
Well, Say we have the following methods:
Now, say we want to pick |
Beta Was this translation helpful? Give feedback.
-
I'm a bigger fan of the syntax |
Beta Was this translation helpful? Give feedback.
-
@HaloFour I don't mind either way but yeah but |
Beta Was this translation helpful? Give feedback.
-
Definitely prefer #712. |
Beta Was this translation helpful? Give feedback.
-
I had a very similar idea, but my use-case is casting.
|
Beta Was this translation helpful? Give feedback.
-
I would also like to declare fields, parameters and variable using
Basically, why not allow |
Beta Was this translation helpful? Give feedback.
-
@busterwood typeof() allows access to details of the type, eg. assembly information, generic attributes, etc. It does not refer to the type in any way usable as a cast operator or instance declaration. That's not to say there is no merit to having a keyword for that functionality, but it should be opened and discussed in a different thread. |
Beta Was this translation helpful? Give feedback.
-
okay, opened #1294 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi C# Team,
I would like to request a few enhancements to the "TypeOf" operator.
Often times I find myself writing code that needs a type provided. Here's a basic example:
I love how the "nameof" operator makes my code refactoring-safe. For example, I can do:
And then my dependency property will always be registered with the same name of the exposed property, even if I refactor my code. Unfortunately, if I change the type of my property, I have a long find-and-replace job ahead of me.
I would like "typeof" to be expanded to allow me to reference other members just like "nameof" does. For example:
You'll notice that this has two enhancements:
typeof(member)
- this enhancement allows typeof(...) to resolve to the DECLARED type of a member/variable. This is not the same as GetType() which requires an instance. For example:The second enhancement is:
typeof(this)
. This is a special case which means "resolve to the type of the containing class". I will point out thatthis
is not thethis
instance.for example:
Beta Was this translation helpful? Give feedback.
All reactions