Declaring Func dynamically

You can do this by using an open generic type definition, and then making the specific type from that: typeof(Func<,>).MakeGenericType(typeof(int), orderType); However, what you’re trying to do (calling Lambda<TDelegate>) is not directly possible. You must call Lambda without a type parameter: var propertyinfo = typeof(T).GetProperty(sortExpressionStr); Type orderType = propertyinfo.PropertyType; var param = Expression.Parameter(typeof(T), “x”); var … Read more

Anonymous method in Invoke call

Because Invoke/BeginInvoke accepts Delegate (rather than a typed delegate), you need to tell the compiler what type of delegate to create ; MethodInvoker (2.0) or Action (3.5) are common choices (note they have the same signature); like so: control.Invoke((MethodInvoker) delegate {this.Text = “Hi”;}); If you need to pass in parameters, then “captured variables” are the … Read more