0

I am trying to only get the string "DateTime" from my Type. But if I return type.Name I get "Nullable'1". If I return type.FullName I get:

System.Nullable`1[[System.DateTime, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]"

Is there no way for me to simply get "DateTime" as a return string?

Green_qaue
  • 3,561
  • 11
  • 47
  • 89
  • 1
    What you have tried? could you please sahre your code? – Ramesh Rajendran May 16 '18 at 10:35
  • `string result = type.GenericTypeArguments[0].Name;` where `Type type = typeof(Nullable);` – Dmitry Bychenko May 16 '18 at 10:35
  • Try this `DateTime xx = DateTime.Now; var type = xx.GetType().GetProperties().ToList()[0].PropertyType.Name` – Ramesh Rajendran May 16 '18 at 10:38
  • 1
    @RameshRajendran That´s a really ugly workaround and I assume won´t help OP much, as he doesn´t seem to know the `DateTime`-type at compile-time. Furthermore there´s no guranatee that the first property of `DateTime` returned by `GetProperties` allways *is* a `DateTime` itself. – MakePeaceGreatAgain May 16 '18 at 10:41
  • I tried `DateTime sample = DateTime.Now; Type t = sample.GetType(); string text = t.Name;` I got the result – Lucifer May 16 '18 at 10:42
  • @Lucifer Please check : https://dotnetfiddle.net/IsUmXd – Ramesh Rajendran May 16 '18 at 10:43
  • 1
    you should share a code sample of yours – Lucifer May 16 '18 at 10:43
  • @HimBromBeere Please check https://dotnetfiddle.net/IsUmXd – Ramesh Rajendran May 16 '18 at 10:44
  • 1
    @RameshRajendran Please don´t edit a deleted answer, in particular not in a fashion that completely different way than the original intention. Apart from this I didn´t say your solution doesn´t work. Only that its ugly and based on `DatTime`-s internal implemenation as well as the implementation of `GetProperties`. – MakePeaceGreatAgain May 16 '18 at 10:46
  • @HimBromBeere I am just testing that. Why SO supports to allow modification for deleted post. :) .. Don't worry i rolled back – Ramesh Rajendran May 16 '18 at 10:47
  • @RameshRajendran: the problem with your code snippet is a) the OP has a `Nullable`, not a `DateTime`. b) What your code is doing is returning the type of the first property of the `DateTime` object, not the generic parameter type which is what the OP wants c) While this returns `DateTime` its not because the original object is a DateTime. It doesn't work for a different object. eg if you tried it for a TimeSpan you would get `Int64` returned. – Chris May 16 '18 at 11:01

0 Answers0