typeof(member) #3473
Replies: 5 comments
-
Would be better served if C# ever got |
Beta Was this translation helpful? Give feedback.
-
I kind of miss the Informix 4GL feature where you could declare a variable based on the type of another variable or member of a structure. Made refactoring very easy. 😄 |
Beta Was this translation helpful? Give feedback.
-
@HaloFour Typescript has that, and it's pretty great. You can pass any expression to const expr = { x: 4, y: 5 }
type T = typeof expr |
Beta Was this translation helpful? Give feedback.
-
Why not Some usage: var x = new { X = 1 }; // <>f__AnonymousType0<<X>j__TPar>
var t = x.GetType().GetConstructor(new [] { typeof(int) });
decltype(x) y = t.Invoke(new object[] { 5 }); // <>f__AnonymousType0<<X>j__TPar>
// Since y is declared as <>f__AnonymousType0<<X>j__TPar> but not object,
// we can have intellisense in the following context,
// and we no longer need reflection to get the value of y.X
Console.WriteLine(y.X); // 5 public interface MyInterface {
string Foo { get; set; }
}
....
decltype(MyInterface.Foo) x = "123"; // string x = "123"
var type = typeof(decltype(MyInterface.Foo)); // typeof(string) |
Beta Was this translation helpful? Give feedback.
-
Good suggestion |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Please allow typeof(...) to return the declared type of a member.
Beta Was this translation helpful? Give feedback.
All reactions