Created
August 26, 2012 18:43
-
-
Save blam23/3482474 to your computer and use it in GitHub Desktop.
TryGet Stuff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public bool TryGetValue<T>(string k, out T value) | |
{ | |
object v; | |
var d = Data.TryGetValue(k, out v); | |
if (d) | |
{ | |
if (v is object[]) | |
{ | |
throw new Exception("TryGetValue does not support arrays, use TryGetArray instead."); | |
} | |
else | |
{ | |
value = (T) Convert.ChangeType(v, typeof (T)); | |
} | |
return true; | |
} | |
value = default(T); | |
return false; | |
} | |
public bool TryGetArray<T>(string k, out T[] value) | |
{ | |
object v; | |
var d = Data.TryGetValue(k, out v); | |
if (d) | |
{ | |
var c = (object[])v; | |
value = new T[c.Length]; | |
for (var i = 0; i < c.Length; i++) | |
{ | |
value[i] = (T)Convert.ChangeType(c[i], typeof(T)); | |
} | |
return true; | |
} | |
value = default(T[]); | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment