में प्रतिबिंब का उपयोग करके पूर्वानुमान करने के लिए func को कनवर्ट करें मैं मूल रूप से this करने की कोशिश कर रहा हूं, लेकिन मुझे नहीं पता कि टी क्या होगा, इसलिए मैं प्रतिबिंब और अभिव्यक्ति पेड़ों का उपयोग करके चीजों का निर्माण कर रहा हूं।सी #
// Input (I don't know about "Book")
Type itemType = typeof(Book);
// Actual Code
// Build up func p => p.AuthorName == "Jon Skeet"
ParameterExpression predParam = Expression.Parameter(itemType, "p");
Expression left = Expression.Field(predParam, itemType.GetField("AuthorName"));
Expression right = Expression.Constant("Jon Skeet", typeof(string));
Expression equality = Expression.Equal(left, right);
Delegate myDelegate = Expression.Lambda(equality, new ParameterExpression[] { predParam }).Compile(); // Not sure if I need this
// Build up predicate type (Predicate<Book>)
Type genericPredicateType = typeof(Predicate<>);
Type constructedPredicateType = genericPredicateType.MakeGenericType(new Type[] { itemType });
// I need an instance to use this predicate, right? (** This Fails **)
object predicateInstance = Activator.CreateInstance(constructedPredicateType, new object[] { myDelegate });
मूल रूप से, मैं एक List<Book>
, जो मैं पर प्रतिबिंबित करने की कोशिश कर रहा हूँ और Invoke
अपने Find
विधि है। Find
विधि को की बजाय Predicate<Book>
की आवश्यकता है, और मैं कुछ घंटों के लिए इसके खिलाफ अपना सिर मार रहा हूं।
संपादित करें:
System.MissingMethodException: Constructor on type 'System.Predicate`1[[MyProject.Book, MyProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' not found.
कोई हिस्सा है जहाँ मैं विधि आह्वान को देखने के लिए की जरूरत है, मैं भी है कि आपूर्ति कर सकते हैं, लेकिन मैंने सोचा कि यह आवश्यक में कोई कमी होगी प्रश्न का हिस्सा –