Get the type from a nullable variable
Clash Royale CLAN TAG#URR8PPP
Get the type from a nullable variable
public class MyType
public int? MyId get; set;
MyType myType = new MyType();
myType.MyId.GetType()
the last line returns an exception since MyId
is not set (ie. it's null). How I get the type (int?
or even int
) in this case? Note, int?
is used as an example, the variable may have any type, this is just a simplified example.
MyId
int?
int
int?
Note, according to Microsoft, this is supposed to work:
int? a = 17;
Type typeOfA = a.GetType();
Console.WriteLine(typeOfA.FullName);
// Output:
// System.Int32
and it does work when the value is assigned...
EDIT.
Looking at some of the replies and comments, I would like to add that in code, I pass myType.MyId as an object to a method that needs to figure out its type. Basically it looks similar to:
public void RunQuery(string sql, List<(string parameterName, object parameterValue)> parameters)
so myType.MyId is passed into RunQuery as parameterValue
GetType()
typeof(MyType).GetProperty("MyId").PropertyType
Nullable<int>
Nullable.GetUnderlyingType(type)
2 Answers
2
You can use reflection to get declared type of a property (which is known at compile time):
Type t = typeof(MyType).GetProperty(nameof(MyType.MyId)).PropertyType;
And GetType()
is used to figure out the actual type of an object in runtime, but that does not make sense for a null
reference.
GetType()
null
Edit:
When you cast Nullable<T>
to an Object
, its value is boxed, so, if it was null
, you will get just an Object
variable with null
reference, and you won't be able to find out the type any more.
Nullable<T>
Object
null
Object
null
So, you should somehow change your infrastructure to make the type be passed with your parameter. The fastest workaround is to pass it explicitly
List<(string parameterName, object parameterValue, Type parameterType)> parameters
Check out System.Data.SqlClient.SqlParameter, I am not sure, but this is probably exactly what you need to use.
so since it's a generic method, I don't even have the information about the class MyType, i'm just passing myType.MyId as a parameter to another method
– user19754
Aug 10 at 18:05
@user19754 then what is the type of this parameter?
– Bagdan Gilevich
Aug 10 at 18:21
it's passed as an object (object parameterValue), so then I can do parameterValue.GetType() to get the type for non-nullable types of parameterValueor for nullable types of parameterValue, where the value is not null. I get an exception when it's null.
– user19754
Aug 10 at 20:13
@user19754 so, when someone passes null, then you have a local
object
variable with null value - there is no way to find out which type it could be– Bagdan Gilevich
Aug 10 at 20:30
object
@user19754 try making your method generic like
Method<T>(T parameterValue)
and obtain your type like Type type = typeof (T);
.– Bagdan Gilevich
Aug 10 at 20:33
Method<T>(T parameterValue)
Type type = typeof (T);
[Edited], I've re-written my solution to address your question: Making a function that is agnostic to MyType:
string GetPropertyName<T>(T prop)
var type = typeof(T);
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
return type.GenericTypeArguments[0].FullName;
else
return type.FullName;
You can now call it like this:
MyType myType = new MyType();
string name = GetPropertyName(myType.MyId);
Console.WriteLine(name); // Outputs System.Int32
I've test it, and it's working for me.
BTW, if you pass it a non-nullable member, it will return that property name. You can test that for yourself.
I don't understand how it helps at all. I don't need to determine if it's a nullable type. In fact, I could just compare the value to null to "deduct" this. I need to know the underlying type as this is a parameter for a db query and i need to declare it with the correct db type.
– user19754
Aug 10 at 17:02
Sorry to give you an incomplete solution. Just added more.
– RunzWitScissors
Aug 10 at 18:17
@user19754, have you been able to try this solution?
– RunzWitScissors
Aug 10 at 21:30
In your code you imply the knowledge about MyType, however, in my case I don't know, myType.MyId is passed as an object parameter. I will edit the question to make it more specific
– user19754
Aug 13 at 12:25
@user19754. I've re-written my answer. I'm not sure if that completely answers your updated question above, but perhaps it points you towards a solution.
– RunzWitScissors
Aug 13 at 14:58
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
GetType()
gets the type of the entity stored in a variable, field or property. If its null, there is no such entity. In that case (I think) you would have to use reflection.typeof(MyType).GetProperty("MyId").PropertyType
should get you the property's type, which will beNullable<int>
. You can pass that intoNullable.GetUnderlyingType(type)
to get the underlying type inside the nullable. For variables (again, I think) you could write a generic method to extract the inner type.– Amy
Aug 10 at 16:42