में इस प्रकार का कास्टिंग करने का कोई तरीका है, मैं एक सामान्य विधि बनाने की कोशिश कर रहा हूं जो किसी XML दस्तावेज़ में तत्व ढूंढने के लिए एक अनुमान वापस कर देगा।क्या सी # भविष्यवाणी
मूल रूप से कुछ इस तरह:
private static Func<XElement, bool> GetPredicate<T>(Criterion criterion)
{
switch (criterion.CriteriaOperator)
{
case CriteriaOperator.Equal:
return x => (T)x.Attribute(criterion.PropertyName) ==
(T)(criterion.PropertyValue);
case CriteriaOperator.GreaterThan:
return x => (T)x.Attribute(criterion.PropertyName) >
(T)(criterion.PropertyValue);
case CriteriaOperator.GreaterThanOrEqual:
return x => (T)x.Attribute(criterion.PropertyName) >=
(T)(criterion.PropertyValue);
case CriteriaOperator.LessThan:
return x => (T)x.Attribute(criterion.PropertyName) <
(T)(criterion.PropertyValue);
case CriteriaOperator.LessThanOrEqual:
return x => (T)x.Attribute(criterion.PropertyName) <=
(T)(criterion.PropertyValue);
case CriteriaOperator.NotEqual:
return x => (T)x.Attribute(criterion.PropertyName) !=
(T)(criterion.PropertyValue);
default:
throw new ArgumentException("Criteria Operator not supported.");
}
}
केवल बात यह है कि इस संकलन नहीं करता है। समस्या (T)x.Attribute(criterion.PropertyName)
हिस्सा है जहां संकलक इंगित करता है पर है:
प्रकार 'System.Xml.Linq.XAttribute' 'टी'
वर्तमान में मेरे पास दो तरीकों टाइप करने के लिए की अभिव्यक्ति नहीं दिया जा सकता है कि एक जैसे हैं जो कि एक को डबल करने के लिए और दूसरे को दशमलव के लिए छोड़ देता है। मैं वास्तव में उस तरह की नकल नहीं करना चाहूंगा।
क्या 'Criterion' है? –
@ डैनियलए। व्हाइट: एक कस्टम क्लास जिसमें बूलियन अभिव्यक्ति के लिए डेटा शामिल है। PropertyName एक स्ट्रिंग है जो एक्सएमएल नोड में विशेषता नाम का प्रतिनिधित्व करती है, जिसके खिलाफ मैं मेल खाता हूं और PropertyValue उस ऑब्जेक्ट के साथ एक ऑब्जेक्ट है जिसे मैं ढूंढ रहा हूं। –