संपादित: मैंने सामान्य कार्यान्वयन को हटा दिया और मूल रूप से बताई गई समस्या के लिए बेहतर फिट करने के लिए इस प्रतिक्रिया को साफ़ कर दिया।
नोट: मार्क ग्रेवेल द्वारा उत्तर शायद सबसे संक्षिप्त है यदि आप सिर्फ प्रकार के पार्स किए गए मान को चाहते हैं। नीचे दिया गया जवाब आपको दिखाता है कि विधि को कैसे प्राप्त करें (यानी, MethodInfo ऑब्जेक्ट और इसे कैसे आमंत्रित करें)।
निम्नलिखित काम करना चाहिए, कम से कम प्रकार है कि bool TryParse (स्ट्रिंग, टी मूल्य) सार्वजनिक स्थैतिक लागू के लिए:
public static class Parsing
{
static MethodInfo findTryParseMethod(Type type)
{
//find member of type with signature 'static public bool TryParse(string, out T)'
BindingFlags access = BindingFlags.Static | BindingFlags.Public;
MemberInfo[] candidates = type.FindMembers(
MemberTypes.Method,
access,
delegate(MemberInfo m, object o_ignored)
{
MethodInfo method = (MethodInfo)m;
if (method.Name != "TryParse") return false;
if (method.ReturnParameter.ParameterType != typeof(bool)) return false;
ParameterInfo[] parms = method.GetParameters();
if (parms.Length != 2) return false;
if (parms[0].ParameterType != typeof(string)) return false;
if (parms[1].ParameterType != type.MakeByRefType()) return false;
if (!parms[1].IsOut) return false;
return true;
}, null);
if (candidates.Length > 1)
{
//change this to your favorite exception or use an assertion
throw new System.Exception(String.Format(
"Found more than one method with signature 'public static bool TryParse(string, out {0})' in type {0}.",
type));
}
if (candidates.Length == 0)
{
//This type does not contain a TryParse method - replace this by your error handling of choice
throw new System.Exception(String.Format(
"Found no method with signature 'public static bool TryParse(string, out {0})' in type {0}.",
type));
}
return (MethodInfo)candidates[0];
}
public static bool TryParse(Type t, string s, out object val)
{
MethodInfo method = findTryParseMethod(t); //can also cache 'method' in a Dictionary<Type, MethodInfo> if desired
object[] oArgs = new object[] { s, null };
bool bRes = (bool)method.Invoke(null, oArgs);
val = oArgs[1];
return bRes;
}
//if you want to use TryParse in a generic syntax:
public static bool TryParseGeneric<T>(string s, out T val)
{
object oVal;
bool bRes = TryParse(typeof(T), s, out oVal);
val = (T)oVal;
return bRes;
}
}
उपयोग निम्न परीक्षण कोड:
public bool test()
{
try
{
object oVal;
bool b = Parsing.TryParse(typeof(int), "123", out oVal);
if (!b) return false;
int x = (int)oVal;
if (x!= 123) return false;
}
catch (System.Exception)
{
return false;
}
try
{
int x;
bool b = Parsing.TryParseGeneric<int>("123", out x);
if (!b) return false;
if (x != 123) return false;
}
catch (System.Exception)
{
return false;
}
try
{
object oVal;
bool b = Parsing.TryParse(typeof(string), "123", out oVal);
//should throw an exception (//no method String.TryParse(string s, out string val)
return false;
}
catch (System.Exception)
{
//should throw an exception
}
return true;
}
}
और में इस का उपयोग अपने मामले:
//input: string s, Config
Type tNum = Type.GetType(Config.numberType);
object oVal;
bool ok = Parsing.TryParse(tNum, s, out oVal);
//oVal is now of type tNum and its value is properly defined if ok == true
वर के उपयोग के बारे में: आप क्या वर करता है की एक गलत धारणा है हो सकता है: यह है एक "संस्करण" प्रकार नहीं है (टाइप ऑब्जेक्ट पहले से ही इसके लिए उपयोग किया जाता है) लेकिन इस प्रकार के लिए घोषणा सिंटैक्स को असाइनमेंट के दाएं तरफ ले जाता है। निम्नलिखित घोषणाओं के बराबर हैं:
var anon = new { Name = "abc", X = 123 };
'var' वर्तमान में एक विधि के दायरे में ही उपलब्ध है। विवरण के लिए http://stackoverflow.com/q/4461597/1086351 देखें – fcrick