yansımasını kullanarak yüklemek için dönüştürün Temel olarak this yapmaya çalışıyorum, ama ne olacağını bilmiyorum, bu yüzden Yansıma ve İfade ağaçları kullanarak bir şeyler yapıyorum.C#
// 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 });
Temelde, bir ben yansıtmaya çalışıyorum List<Book>
ve Invoke
onun Find
yöntem var. Find
yönteminin Func<Book, bool>
yerine Predicate<Book>
'a ihtiyacı var ve kafamı bir kaç saatliğine atıyorum.
Düzenleme:
System.MissingMethodException: Constructor on type 'System.Predicate`1[[MyProject.Book, MyProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' not found.
, ben de o sağlayabilmektedir, ama esas olumsuz etkisi olacağını düşündüm sorunun bir parçası. –