While there are similar questions in Stackoverflow about this it does not help me with my problem.
Here is the big picture of what I am doing. I am generating IL for my iDB2DataReader to dynamically get my types from a database and map to my poco.
I am having problems getting a nullable to pull in the data.
So I have a method that needs to return the methodinfo via reflection. To get the this I am using “getmethod” of the type that I need.
Here is the code:
private static MethodInfo GetDataMethod(Type destinationDataType, Type underlyingDestinationDataType, iDB2DataReader reader)
{
MethodInfo methInfo = null;
if (_readerDataMethods.ContainsKey(destinationDataType))
{
methInfo = _readerDataMethods[destinationDataType];
}
else
{
if (underlyingDestinationDataType != null)
{
//trying to get underlying type which would be DateTime thus resulting in GetDatetime.
methInfo = reader.GetType().GetMethod("Get" + underlyingDestinationDataType.Name);
}
else
methInfo = reader.GetType().GetMethod("Get" + destinationDataType.Name);
//methInfo = reader.GetType().GetMethod("Get" + destinationDataType.ToGenericTypeString());
if (methInfo != null)
{
_readerDataMethods[destinationDataType] = methInfo;
}
}
return methInfo;
}
As you can see from my code comment I get the underlying type of datetime but this does not work, it gets a runtime error of “Operation could destabilize the runtime.”.
The real problem is I don’t know what the name I should be using for the getmethod for a Nullable<DateTime>. Or at least I was hoping it was that simple. Any help would be appreciated.
You’re probably going to have to create a special case fur Nullables as the actual type name wouldn’t be valid in a method name. I’d check to see if it’s a Nullable type and the use reflection to get the generic parameter.
Example: