Create an Instruction for Type - c#

Using Mono.Cecil, given this method
private Instruction LoadOnStack(MetadataType type, object value)
{
switch (type)
{
case MetadataType.String:
return _processor.Create(OpCodes.Ldstr, (string) value);
case MetadataType.Int32:
return _processor.Create(OpCodes.Ldc_I4, (Int32) value);
case MetadataType.Int64:
return _processor.Create(OpCodes.Ldc_I8, (Int64) value);
case MetadataType.Boolean:
return _processor.Create(OpCodes.Ldc_I4, (bool) value ? 1 : 0);
}
throw new NotSupportedException("Not a supported primitve parameter type: " + type);
}
How can I create an Instruction that can load value, when value is of type Type?
I notice when value is of type Type that I can test it for it like so :
if (value is TypeReference)
return _processor.Create(???, ???);
But I can not figure out what I need to pass to Create to get the value to load correctly.
EDIT:
Using this :
if (value is TypeReference)
return _processor.Create(OpCodes.Ldobj, type.Resolve());
Gets me one step closer. It seems to to accept the type. But then when I try to write the assembly, it errors out saying :
System.ArgumentException : Member 'System.Type' is declared in another module and needs to be imported

As #cubrr already pointed it out:
We use this code for MethodBoundaryAspect.Fody
private IList<Instruction> LoadValueOnStack(TypeReference parameterType, object value, ModuleDefinition module)
{
if (parameterType.IsPrimitive || (parameterType.FullName == "System.String"))
return new List<Instruction> {LoadPrimitiveConstOnStack(parameterType.MetadataType, value)};
if (parameterType.IsValueType) // enum
{
var enumUnderlyingType = GetEnumUnderlyingType(parameterType.Resolve());
return new List<Instruction> {LoadPrimitiveConstOnStack(enumUnderlyingType.MetadataType, value)};
}
if (parameterType.FullName == "System.Type")
{
var typeName = value.ToString();
var typeReference = module.GetType(typeName, true);
var typeTypeRef = _referenceFinder.GetTypeReference(typeof (Type));
var methodReference = _referenceFinder.GetMethodReference(typeTypeRef, md => md.Name == "GetTypeFromHandle");
var instructions = new List<Instruction>
{
_processor.Create(OpCodes.Ldtoken, typeReference),
_processor.Create(OpCodes.Call, methodReference)
};
return instructions;
}
throw new NotSupportedException("Parametertype: " + parameterType);
}
private Instruction LoadPrimitiveConstOnStack(MetadataType type, object value)
{
switch (type)
{
case MetadataType.String:
return _processor.Create(OpCodes.Ldstr, (string) value);
case MetadataType.Int32:
return _processor.Create(OpCodes.Ldc_I4, (int) value);
case MetadataType.Int64:
return _processor.Create(OpCodes.Ldc_I8, (long) value);
case MetadataType.Boolean:
return _processor.Create(OpCodes.Ldc_I4, (bool) value ? 1 : 0);
}
throw new NotSupportedException("Not a supported primitive parameter type: " + type);
}
private static TypeReference GetEnumUnderlyingType(TypeDefinition self)
{
foreach (var field in self.Fields)
{
if (field.Name == "value__")
return field.FieldType;
}
throw new ArgumentException();
}
where class ReferenceFinder is:
private readonly ModuleDefinition _moduleDefinition;
public ReferenceFinder(ModuleDefinition moduleDefinition)
{
_moduleDefinition = moduleDefinition;
}
public MethodReference GetMethodReference(Type declaringType, Func<MethodDefinition, bool> predicate)
{
return GetMethodReference(GetTypeReference(declaringType), predicate);
}
public MethodReference GetMethodReference(TypeReference typeReference, Func<MethodDefinition, bool> predicate)
{
var typeDefinition = typeReference.Resolve();
MethodDefinition methodDefinition;
do
{
methodDefinition = typeDefinition.Methods.FirstOrDefault(predicate);
typeDefinition = typeDefinition.BaseType == null
? null
: typeDefinition.BaseType.Resolve();
} while (methodDefinition == null && typeDefinition != null);
return _moduleDefinition.Import(methodDefinition);
}
public TypeReference GetTypeReference(Type type)
{
return _moduleDefinition.Import(type);
}

Related

Implement Generic Get<T> for MemoryCache (or any Cache)

I'm trying to write a "simple" Generic Get<T>; extension for
System.Runtime.MemoryCache.
Why "simple" ? Because generally I know object's real type before caching it, so when I retrieve it from cache, I'm not going to convert it in unpredictable ways.
For example: if boolean "true" value is stored in cache with cacheKey "id", so
Get<string>("id") == "true";
Get<int>("id") == 1; // any result > 0 is okay
Get<SomeUnpredictableType> == null; // just ignore these trouble conversions
Here's my incomplete implemention:
public static T DoGet<T>(this MemoryCache cache, string key) {
object value = cache.Get(key);
if (value == null) {
return default(T);
}
if (value is T) {
return (T)value;
}
// TODO: (I'm not sure if following logic is okay or not)
// 1. if T and value are both numeric type (e.g. long => double), how to code it?
// 2. if T is string, call something like Convert.ToString()
Type t = typeof(T);
t = (Nullable.GetUnderlyingType(t) ?? t);
if (typeof(IConvertible).IsAssignableFrom(value.GetType())) {
return (T)Convert.ChangeType(value, t);
}
return default(T);
}
Any suggestions are highly appreciated.
===================================
Update (04/11/2016):
For those nice suggestions given, I implement my first version of Get<T>
public class MemCache {
private class LazyObject<T> : Lazy<T> {
public LazyObject(Func<T> valueFactory) : base(valueFactory) { }
public LazyObject(Func<T> valueFactory, LazyThreadSafetyMode mode) : base(valueFactory, mode) { }
}
private static T CastValue<T>(object value) {
if (value == null || value is DBNull) {
return default(T);
}
Type valType = value.GetType();
if (valType.IsGenericType && valType.GetGenericTypeDefinition() == typeof(LazyObject<>)) {
return CastValue<T>(valType.GetProperty("Value").GetValue(value));
}
if (value is T) {
return (T)value;
}
Type t = typeof(T);
t = (Nullable.GetUnderlyingType(t) ?? t);
if (typeof(IConvertible).IsAssignableFrom(t) && typeof(IConvertible).IsAssignableFrom(value.GetType())) {
return (T)Convert.ChangeType(value, t);
}
return default(T);
}
private MemoryCache m_cache;
public T Get<T>(string key) {
return CastValue<T>(m_cache.Get(key));
}
public void Set<T>(string key, T value, CacheDependency dependency) {
m_cache.Set(key, value, dependency.AsCacheItemPolicy());
}
public T GetOrAdd<T>(string key, Func<T> fnValueFactory, CacheDependency dependency) {
LazyObject<T> noo = new LazyObject<T>(fnValueFactory, LazyThreadSafetyMode.ExecutionAndPublication);
LazyObject<T> old = m_cache.AddOrGetExisting(key, noo, dependency.AsCacheItemPolicy()) as LazyObject<T>;
try {
return CastValue<T>((old ?? noo).Value);
} catch {
m_cache.Remove(key);
throw;
}
}
/* Remove/Trim ... */
}
The essential work is to write a CastValue<T> to convert any object to desired type. And it doesn't have to handle very complicate condition because object types in cache is predictable for the programmer. And here's my version.
public static T CastValue<T>(object value) {
if (value == null || value is DBNull) {
return default(T);
}
if (value is T) {
return (T)value;
}
Type t = typeof(T);
t = (Nullable.GetUnderlyingType(t) ?? t);
if (typeof(IConvertible).IsAssignableFrom(t) && typeof(IConvertible).IsAssignableFrom(value.GetType())) {
return (T)Convert.ChangeType(value, t);
}
return default(T);
}
Proposal:
public static T DoGet<T>(this MemoryCache cache, string key)
{
object value = cache.Get(key);
if (value == null) {
return default(T);
}
// support for nullables. Do not waste performance with
// type conversions if it is not a nullable.
var underlyingType = Nullable.GetUnderlyingType(t);
if (underlyingType != null)
{
value = Convert.ChangeType(value, underlyingType);
}
return (T)value;
}
Usage (supposed you have an id of type int in the cache):
int id = Get<int>("id");
int? mayBeId = Get<int?>("id");
string idAsString = Get<int?>("id")?.ToString();
double idAsDouble = (double)Get<int>("id");
I haven't test it.

Populating nullable type from SqlDataReader using Reflection.Emit

When trying to set value for any nullable datatype using reflection.emit, the values for nullable data types are not populated.
for e.g.
public class Employee
{
public int Id { get;set;}
public string? Name {get;set;}
public DateTime? Joined {get;set;}
public bool? IsManager {get;set;}
}
Here are my reflection.emit code to populate sql data reader:
private static readonly MethodInfo GetValueMethod = typeof(SqlDataReader).GetMethod("get_Item", new Type[] { typeof(int) });
private static readonly MethodInfo IsDBNullMethod = typeof(SqlDataReader).GetMethod("IsDBNull", new Type[] { typeof(int) });
DynamicMethod method = new DynamicMethod("DynamicCreateMapping", typeof(T), new Type[] { typeof(IDataRecord) }, typeof(T), true);
ILGenerator generator = method.GetILGenerator();
LocalBuilder result = generator.DeclareLocal(typeof(T));
generator.Emit(OpCodes.Newobj, typeof(T).GetConstructor(Type.EmptyTypes));
generator.Emit(OpCodes.Stloc, result);
for (int i = 0; i < reader.FieldCount; i++)
{
PropertyInfo propertyInfo = typeof(T).GetProperty(reader.GetName(i));
Label endIfLabel = generator.DefineLabel();
if (propertyInfo != null && propertyInfo.GetSetMethod() != null)
{
generator.Emit(OpCodes.Ldarg_0);
generator.Emit(OpCodes.Ldc_I4, i);
generator.Emit(OpCodes.Callvirt, IsDBNullMethod);
generator.Emit(OpCodes.Brtrue, endIfLabel);
generator.Emit(OpCodes.Ldloc, result);
generator.Emit(OpCodes.Ldarg_0);
generator.Emit(OpCodes.Ldc_I4, i);
generator.Emit(OpCodes.Callvirt, GetValueMethod);
generator.Emit(OpCodes.Unbox_Any, reader.GetFieldType(i));
generator.Emit(OpCodes.Callvirt, propertyInfo.GetSetMethod());
generator.MarkLabel(endIfLabel);
}
}
generator.Emit(OpCodes.Ldloc, result);
generator.Emit(OpCodes.Ret);
I know the problem exist on the below line but not sure how to fix it:
generator.Emit(OpCodes.Unbox_Any, reader.GetFieldType(i));
I try to set but it failed using:
generator.Emit(OpCodes.Unbox_Any, propertyInfo.PropertyType);
So how to match the datatype from class and not from the sql data reader?
I fixed it after breaking head for quite sometime.
Thanks to the link [http://tillitsonpaper.blogspot.co.uk/2011/05/mapping-data-reader-to-object.html][1].
I did modified few codes and am pasting it here so that it might help newbie who has very little knowledge of reflection.Emit like me.
I added condition to check for Boolean and Enum types:
if (propertyInfo.PropertyType.IsEnum || propertyInfo.PropertyType == typeof(Boolean))
{
generator.Emit(OpCodes.Unbox_Any, reader.GetFieldType(i));
}
else
{
generator.Emit(OpCodes.Call, GetConverterMethod(propertyInfo.PropertyType));
}
The else part above will handle any type including nullable types, The GetConverterMethod implementation is shown below:
private MethodInfo GetConverterMethod(Type type)
{
switch (type.Name.ToUpper())
{
case "INT16":
return CreateConverterMethodInfo("ToInt16");
case "INT32":
return CreateConverterMethodInfo("ToInt32");
case "INT64":
return CreateConverterMethodInfo("ToInt64");
case "SINGLE":
return CreateConverterMethodInfo("ToSingle");
case "BOOLEAN":
return CreateConverterMethodInfo("ToBoolean");
case "STRING":
return CreateConverterMethodInfo("ToString");
case "DATETIME":
return CreateConverterMethodInfo("ToDateTime");
case "DECIMAL":
return CreateConverterMethodInfo("ToDecimal");
case "DOUBLE":
return CreateConverterMethodInfo("ToDouble");
case "GUID":
return CreateConverterMethodInfo("ToGuid");
case "BYTE[]":
return CreateConverterMethodInfo("ToBytes");
case "BYTE":
return CreateConverterMethodInfo("ToByte");
case "NULLABLE`1":
{
if (type == typeof(DateTime?))
{
return CreateConverterMethodInfo("ToDateTimeNull");
}
else if (type == typeof(Int32?))
{
return CreateConverterMethodInfo("ToInt32Null");
}
else if (type == typeof(Boolean?))
{
return CreateConverterMethodInfo("ToBooleanNull");
}
break;
}
}
return null;
}
private MethodInfo CreateConverterMethodInfo(string method)
{
return typeof(Converter).GetMethod(method, new Type[] { typeof(object) });
}
The Converter Class code is below:
public class Converter
{
public static Int16 ToInt16(object value)
{
return ChangeType<Int16>(value);
}
public static Int32 ToInt32(object value)
{
return ChangeType<Int32>(value);
}
public static Int64 ToInt64(object value)
{
return ChangeType<Int64>(value);
}
public static Single ToSingle(object value)
{
return ChangeType<Single>(value);
}
public static Boolean ToBoolean(object value)
{
return ChangeType<Boolean>(value);
}
public static System.String ToString(object value)
{
return ChangeType<System.String>(value);
}
public static DateTime ToDateTime(object value)
{
return ChangeType<DateTime>(value);
}
public static Decimal ToDecimal(object value)
{
return ChangeType<Decimal>(value);
}
public static Double ToDouble(object value)
{
return ChangeType<Double>(value);
}
public static Guid ToGuid(object value)
{
return ChangeType<Guid>(value);
}
public static Byte ToByte(object value)
{
return ChangeType<Byte>(value);
}
public static Byte[] ToBytes(object value)
{
return ChangeType<Byte[]>(value);
}
public static DateTime? ToDateTimeNull(object value)
{
return ChangeType<DateTime?>(value);
}
public static System.Int32? ToInt32Null(object value)
{
return ChangeType<Int32?>(value);
}
public static Boolean? ToBooleanNull(object value)
{
return ChangeType<Boolean?>(value);
}
private static T ChangeType<T>(object value)
{
var t = typeof(T);
if (t.IsGenericType && t.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
{
if (value == null)
{
return default(T);
}
t = Nullable.GetUnderlyingType(t); ;
}
return (T)Convert.ChangeType(value, t);
}
}
Might not be very elegant way of doing but this works for any nullable types as well.
Hope this helps to someone who is struggling to get Nullable types working with Reflection.Emit.

Invalid cast from 'System.Int32' to 'System.Nullable`1[[System.Int32, mscorlib]]

Type t = typeof(int?); //will get this dynamically
object val = 5; //will get this dynamically
object nVal = Convert.ChangeType(val, t);//getting exception here
I am getting InvalidCastException in above code. For above I could simply write int? nVal = val, but above code is executing dynamically.
I am getting a value(of non nullable type like int, float, etc) wrapped up in an object (here val), and I have to save it to another object by casting it to another type(which can or cannot be nullable version of it). When
Invalid cast from 'System.Int32' to 'System.Nullable`1[[System.Int32,
mscorlib, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089]]'.
An int, should be convertible/type-castable to nullable int, what is the issue here ?
You have to use Nullable.GetUnderlyingType to get underlying type of Nullable.
This is the method I use to overcome limitation of ChangeType for Nullable
public static T ChangeType<T>(object value)
{
var t = typeof(T);
if (t.IsGenericType && t.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
{
if (value == null)
{
return default(T);
}
t = Nullable.GetUnderlyingType(t);
}
return (T)Convert.ChangeType(value, t);
}
non generic method:
public static object ChangeType(object value, Type conversion)
{
var t = conversion;
if (t.IsGenericType && t.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
{
if (value == null)
{
return null;
}
t = Nullable.GetUnderlyingType(t);
}
return Convert.ChangeType(value, t);
}
For above I could simply write int? nVal = val
Actually, you can't do that either. There is no implicit conversion from object to Nullable<int>. But there is an implicit conversion from int to Nullable<int> so you can write this:
int? unVal = (int)val;
You can use Nullable.GetUnderlyingType method.
Returns the underlying type argument of the specified nullable type.
A generic type definition is a type declaration, such as Nullable,
that contains a type parameter list, and the type parameter list
declares one or more type parameters. A closed generic type is a type
declaration where a particular type is specified for a type parameter.
Type t = typeof(int?); //will get this dynamically
Type u = Nullable.GetUnderlyingType(t);
object val = 5; //will get this dynamically
object nVal = Convert.ChangeType(val, u);// nVal will be 5
Here's a DEMO.
I think I should explain why the function does not work:
1- The line that throw the exception is as follows:
throw new InvalidCastException(Environment.GetResourceString("InvalidCast_FromTo", new object[]
{
value.GetType().FullName,
targetType.FullName
}));
in fact the function search in the array Convert.ConvertTypes after that it see if the targer is an Enum and when nothing is found it throw the exception above.
2- the Convert.ConvertTypes is initialized as:
Convert.ConvertTypes = new RuntimeType[]
{
(RuntimeType)typeof(Empty),
(RuntimeType)typeof(object),
(RuntimeType)typeof(DBNull),
(RuntimeType)typeof(bool),
(RuntimeType)typeof(char),
(RuntimeType)typeof(sbyte),
(RuntimeType)typeof(byte),
(RuntimeType)typeof(short),
(RuntimeType)typeof(ushort),
(RuntimeType)typeof(int),
(RuntimeType)typeof(uint),
(RuntimeType)typeof(long),
(RuntimeType)typeof(ulong),
(RuntimeType)typeof(float),
(RuntimeType)typeof(double),
(RuntimeType)typeof(decimal),
(RuntimeType)typeof(DateTime),
(RuntimeType)typeof(object),
(RuntimeType)typeof(string)
};
So since the int? is not in the ConvertTypes array and not an Enum the exception is thrown.
So to resume, for the function Convert.ChnageType to work you have:
The object to be converted is IConvertible
The target type is within the ConvertTypes and not Empty nor DBNull (There is an explict test on those two with throw exception)
This behaviour is because int (and all other default types) uses Convert.DefaultToType as IConvertibale.ToType implementation. and here is the code of theDefaultToTypeextracted using ILSpy
internal static object DefaultToType(IConvertible value, Type targetType, IFormatProvider provider)
{
if (targetType == null)
{
throw new ArgumentNullException("targetType");
}
RuntimeType left = targetType as RuntimeType;
if (left != null)
{
if (value.GetType() == targetType)
{
return value;
}
if (left == Convert.ConvertTypes[3])
{
return value.ToBoolean(provider);
}
if (left == Convert.ConvertTypes[4])
{
return value.ToChar(provider);
}
if (left == Convert.ConvertTypes[5])
{
return value.ToSByte(provider);
}
if (left == Convert.ConvertTypes[6])
{
return value.ToByte(provider);
}
if (left == Convert.ConvertTypes[7])
{
return value.ToInt16(provider);
}
if (left == Convert.ConvertTypes[8])
{
return value.ToUInt16(provider);
}
if (left == Convert.ConvertTypes[9])
{
return value.ToInt32(provider);
}
if (left == Convert.ConvertTypes[10])
{
return value.ToUInt32(provider);
}
if (left == Convert.ConvertTypes[11])
{
return value.ToInt64(provider);
}
if (left == Convert.ConvertTypes[12])
{
return value.ToUInt64(provider);
}
if (left == Convert.ConvertTypes[13])
{
return value.ToSingle(provider);
}
if (left == Convert.ConvertTypes[14])
{
return value.ToDouble(provider);
}
if (left == Convert.ConvertTypes[15])
{
return value.ToDecimal(provider);
}
if (left == Convert.ConvertTypes[16])
{
return value.ToDateTime(provider);
}
if (left == Convert.ConvertTypes[18])
{
return value.ToString(provider);
}
if (left == Convert.ConvertTypes[1])
{
return value;
}
if (left == Convert.EnumType)
{
return (Enum)value;
}
if (left == Convert.ConvertTypes[2])
{
throw new InvalidCastException(Environment.GetResourceString("InvalidCast_DBNull"));
}
if (left == Convert.ConvertTypes[0])
{
throw new InvalidCastException(Environment.GetResourceString("InvalidCast_Empty"));
}
}
throw new InvalidCastException(Environment.GetResourceString("InvalidCast_FromTo", new object[]
{
value.GetType().FullName,
targetType.FullName
}));
}
in other hand the cast is implemented by Nullable class itself and the definition is:
public static implicit operator T?(T value)
{
return new T?(value);
}
public static explicit operator T(T? value)
{
return value.Value;
}

Converting and Casting an object based on a [Type] object

Hopefully a simple enough question, but I am struggling to find an answer.
How can I cast an 'object' to a type defined in a 'Type' object.
For example, basically I am trying to do the following with reflection, making it as generic as possible:
public class MyClass
{
public string Prop1 { get; set; }
public int Prop2 { get; set; }
}
public T FillMyClass<T>() where T : new()
{
//The definitions here are suppled in code - each class we want to work with needs to be "mapped".
string name1 = "Prop1";
Type type1 = typeof(string);
string name2 = "Prop2";
Type type2 = typeof(int);
//The values always start out as a string because of the way I'm receiving it.
string val1 = "test";
string val2 = "1";
T t= new T();
//Works fine, because val1 is already a string
t.GetType().GetProperty(name1).SetValue(t, val1, null);
//Having trouble with the below.
object o = Convert.ChangeType(val2, type2);
//Fails because o is not an int
t.GetType().GetProperty(name2).SetValue(t, o, null);
}
So the type is defined by the user (or possibly even just by looking up the type of the property).
But I just can't see how to cast the object to a [Type].
I've come across this issue as well a couple of times. While I'm sure there is a more elegant solution to this problem, I've made the following extension method to get you 99% of the way there.
public static object TryConvertToType(this object source, Type destinationType, object defaultValue = null)
{
try
{
if (source == null)
return defaultValue;
if (destinationType == typeof(bool))
{
bool returnValue = false;
if (!bool.TryParse(source.ToString(), out returnValue))
{
return Convert.ChangeType(source.ToString() == "1", destinationType);
}
else
{
return Convert.ChangeType(returnValue, destinationType);
}
}
else if (destinationType.IsSubclassOf(typeof(Enum)))
{
try
{
return Enum.Parse(destinationType, source.ToString());
}
catch
{
return Enum.ToObject(destinationType, source);
}
}
else if (destinationType == typeof(Guid))
{
return Convert.ChangeType(new Guid(source.ToString().ToUpper()), destinationType);
}
else if (destinationType.IsGenericType && destinationType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
Type genericType = destinationType.GetGenericArguments().First();
return Convert.ChangeType(source, genericType);
}
else if (source.GetType().IsSubclassOf(destinationType))
{
return Convert.ChangeType(source, destinationType);
}
else if (!source.GetType().IsValueType
&& source.GetType() != typeof(string)
&& destinationType == typeof(string))
{
return Convert.ChangeType(source.GetType().Name, destinationType);
}
else
{
return Convert.ChangeType(source, destinationType);
}
}
catch
{
return defaultValue;
}
}
Basically, in usage, it works like so:
t.GetType().GetProperty(name).GetValue(t, null).TryConvertToType(type2, 0);

Creating a generic method in C#

I am trying to combine a bunch of similar methods into a generic method. I have several methods that return the value of a querystring, or null if that querystring does not exist or is not in the correct format. This would be easy enough if all the types were natively nullable, but I have to use the nullable generic type for integers and dates.
Here's what I have now. However, it will pass back a 0 if a numeric value is invalid, and that unfortunately is a valid value in my scenarios. Can somebody help me out? Thanks!
public static T GetQueryString<T>(string key) where T : IConvertible
{
T result = default(T);
if (String.IsNullOrEmpty(HttpContext.Current.Request.QueryString[key]) == false)
{
string value = HttpContext.Current.Request.QueryString[key];
try
{
result = (T)Convert.ChangeType(value, typeof(T));
}
catch
{
//Could not convert. Pass back default value...
result = default(T);
}
}
return result;
}
What if you specified the default value to return, instead of using default(T)?
public static T GetQueryString<T>(string key, T defaultValue) {...}
It makes calling it easier too:
var intValue = GetQueryString("intParm", Int32.MinValue);
var strValue = GetQueryString("strParm", "");
var dtmValue = GetQueryString("dtmPatm", DateTime.Now); // eg use today's date if not specified
The downside being you need magic values to denote invalid/missing querystring values.
I know, I know, but...
public static bool TryGetQueryString<T>(string key, out T queryString)
What about this? Change the return type from T to Nullable<T>
public static Nullable<T> GetQueryString<T>(string key) where T : struct, IConvertible
{
T result = default(T);
if (String.IsNullOrEmpty(HttpContext.Current.Request.QueryString[key]) == false)
{
string value = HttpContext.Current.Request.QueryString[key];
try
{
result = (T)Convert.ChangeType(value, typeof(T));
}
catch
{
//Could not convert. Pass back default value...
result = default(T);
}
}
return result;
}
Convert.ChangeType() doesn't correctly handle nullable types or enumerations in .NET 2.0 BCL (I think it's fixed for BCL 4.0 though). Rather than make the outer implementation more complex, make the converter do more work for you. Here's an implementation I use:
public static class Converter
{
public static T ConvertTo<T>(object value)
{
return ConvertTo(value, default(T));
}
public static T ConvertTo<T>(object value, T defaultValue)
{
if (value == DBNull.Value)
{
return defaultValue;
}
return (T) ChangeType(value, typeof(T));
}
public static object ChangeType(object value, Type conversionType)
{
if (conversionType == null)
{
throw new ArgumentNullException("conversionType");
}
// if it's not a nullable type, just pass through the parameters to Convert.ChangeType
if (conversionType.IsGenericType && conversionType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
{
// null input returns null output regardless of base type
if (value == null)
{
return null;
}
// it's a nullable type, and not null, which means it can be converted to its underlying type,
// so overwrite the passed-in conversion type with this underlying type
conversionType = Nullable.GetUnderlyingType(conversionType);
}
else if (conversionType.IsEnum)
{
// strings require Parse method
if (value is string)
{
return Enum.Parse(conversionType, (string) value);
}
// primitive types can be instantiated using ToObject
else if (value is int || value is uint || value is short || value is ushort ||
value is byte || value is sbyte || value is long || value is ulong)
{
return Enum.ToObject(conversionType, value);
}
else
{
throw new ArgumentException(String.Format("Value cannot be converted to {0} - current type is " +
"not supported for enum conversions.", conversionType.FullName));
}
}
return Convert.ChangeType(value, conversionType);
}
}
Then your implementation of GetQueryString<T> can be:
public static T GetQueryString<T>(string key)
{
T result = default(T);
string value = HttpContext.Current.Request.QueryString[key];
if (!String.IsNullOrEmpty(value))
{
try
{
result = Converter.ConvertTo<T>(value);
}
catch
{
//Could not convert. Pass back default value...
result = default(T);
}
}
return result;
}
You can use sort of Maybe monad (though I'd prefer Jay's answer)
public class Maybe<T>
{
private readonly T _value;
public Maybe(T value)
{
_value = value;
IsNothing = false;
}
public Maybe()
{
IsNothing = true;
}
public bool IsNothing { get; private set; }
public T Value
{
get
{
if (IsNothing)
{
throw new InvalidOperationException("Value doesn't exist");
}
return _value;
}
}
public override bool Equals(object other)
{
if (IsNothing)
{
return (other == null);
}
if (other == null)
{
return false;
}
return _value.Equals(other);
}
public override int GetHashCode()
{
if (IsNothing)
{
return 0;
}
return _value.GetHashCode();
}
public override string ToString()
{
if (IsNothing)
{
return "";
}
return _value.ToString();
}
public static implicit operator Maybe<T>(T value)
{
return new Maybe<T>(value);
}
public static explicit operator T(Maybe<T> value)
{
return value.Value;
}
}
Your method would look like:
public static Maybe<T> GetQueryString<T>(string key) where T : IConvertible
{
if (String.IsNullOrEmpty(HttpContext.Current.Request.QueryString[key]) == false)
{
string value = HttpContext.Current.Request.QueryString[key];
try
{
return (T)Convert.ChangeType(value, typeof(T));
}
catch
{
//Could not convert. Pass back default value...
return new Maybe<T>();
}
}
return new Maybe<T>();
}
I like to start with a class like this
class settings
{
public int X {get;set;}
public string Y { get; set; }
// repeat as necessary
public settings()
{
this.X = defaultForX;
this.Y = defaultForY;
// repeat ...
}
public void Parse(Uri uri)
{
// parse values from query string.
// if you need to distinguish from default vs. specified, add an appropriate property
}
This has worked well on 100's of projects. You can use one of the many other parsing solutions to parse values.

Categories