CreateDelegate On Extension Method - c#

I have a class with an IList readonly property. I have created a simple extension method, AddCSV, to add multiple items to that list. I want to create an action delegate to populate the list via the extension method. So far, I have
private Action<TListPropertyContainer, TDataValue> CreateListPropertySetter<TListPropertyContainer, TDataValue>(string listName)
{
var list = typeof(TListPropertyContainer).GetProperty(listName);
var method = typeof(Extensions).GetMethod("AddCSV");
return (Action<TListPropertyContainer, TDataValue>)Delegate.CreateDelegate(typeof(Action<TListPropertyContainer, TDataValue>), list, method);
}
but obviously this isn't working!
I am aware that there are other options. For example
a) I could inherit the list into my own customer class and add the AddCSV there
b) I could make items property read/write and set a fully populated list into my class
I'd be grateful if someone could correct me.
Many thx
Simon

There are two main problems.
You are trying to invoke the method on a PropertyInfo, not a list. To get the value of the property you would need to make a call to GetValue()
The call to GetMethod() doesn't specify binding flags. I suspect it might work better with GetMethod("AddCSV", BindingFlags.Public | BindingFlags.Static).
That being said, why are you instantiating it reflectively when you know the type and the method beforehand? It seems like you could just do:
private Action<TListPropertyContainer, TDataValue> CreateListPropertySetter<TListPropertyContainer, TDataValue>(string listName)
{
var propertyInfo = typeof(TListPropertyContainer).GetProperty(listName);
return (container,value) => {
var list = (IList<TDataValue>)propertyInfo.GetValue(container,null);
list.AddCSV(list);
};
}
If I am making incorrect assumptions about the signature of the extension method or the type of property, you can still do it with Delegate.CreateDelegate(), but take the comments about the PropertyInfo and the BindingFlags into account

You're trying to use list as the target of the delegate - but list is of type PropertyInfo, which sounds like it's not what you were expecting. Assuming you want to get the value of the property, and then call the method on that, you'll need to pass in the object containing the property as well, so you can get the actual list. (Alternatively, maybe it's "this" - you haven't really made that clear.) Either way, you can get the list itself and use that as the target. For example:
private Action<TListPropertyContainer, TDataValue>
CreateListPropertySetter<TListPropertyContainer, TDataValue>
(string listName, object target)
{
var listProperty = typeof(TListPropertyContainer).GetProperty(listName);
object list = listProperty.GetValue(target, null);
var method = typeof(Extensions).GetMethod("AddCSV");
return (Action<TListPropertyContainer, TDataValue>)Delegate.CreateDelegate(
typeof(Action<TListPropertyContainer, TDataValue>), list, method);
}
If this doesn't help, please edit your question with a short but complete console app demonstrating the problem. Right now there are too many unknowns to definitely help you.

Related

Cast object to List<object>

For some reasons outside of my control a method of mine needs to be defined to accept one parameter of type object. But i know that the value of this parameter is actually of type List with a generic parameter unknown to me.
I am trying to cast this object to List<object>, but have no idea how to do this without knowing what the exact generic parameter is.
I would like to do something like this:
public static List<object> ConvertToList(object input)
{
if (input.GetType().GetGenericTypeDefinition().IsAssignableFrom(typeof(List<>)))
{
//Of course this does not work, because input might for example be of type List<string>
return (List<object>) input;
}
else
{
return null;
}
}
I know that if I had List<T> instead of object input, it would be quite easy by using:
return input.Cast<object>()
but unfortunately i'm stuck with the object parameter. Any idea how i could solve this problem?
You can't cast it, but you can project it onto a list using IEnumerable and Cast. This will give you a new instance of List<object>. You won't be able to modify the old list but you can work with the new one and modify its elements.
object obj = new List<Whatever>();
List<object> list = ((IEnumerable)obj).Cast<object>().ToList();

C# passing a list of strongly typed property names

I'm looking for a way to pass in a list of strongly typed property names into a method that I can then dissect and get the properties that the caller is interested in. The reason I want to do this is to have a copy method that only copies the fields the user specifies. Right now, the method takes a list of strings to use with the Getvalues and get properties methods in reflection, but I want to guard against refactoring of properties and the strings not being updated by the developer.
I found this article Here, but unfortunately, it does not do a list. I can do something like:
public static void Copy(Expression<Func<TObject>> propertiesToCopy )
{
}
And then have the caller do
PropertyCopier<List<string>>.Copy(() => data);
But then I have to specify how many properties the caller can have like this:
public static void Copy(Expression<Func<TObject>> propertiesToCopy,Expression<Func<TObject>> propertiesToCopy2, Expression<Func<TObject>> propertiesToCopy3 )
{
}
This would allow for three properties. Is there anyway to add it to a List or Queryable<> to allow as many properties as the caller wants? I tried using the Add in List and having the Expression
Thanks in advance
Edit: I did find a few articles this evening that refer to using the C# param keyword to accomplish this. Are there any better or more efficient ways, or is this the best way to do it?
Use the params keyword to define a method that takes a variable number of arguments:
public static void PrintPropertyNames<T>(params Expression<Func<T, object>>[] properties)
{
foreach (var p in properties)
{
var expression = (MemberExpression)((UnaryExpression)p.Body).Operand;
string memberName = expression.Member.Name;
Console.WriteLine(memberName);
}
}
For instance, you could call the PrintPropertyNames method passing two expressions:
PrintPropertyNames<FileInfo>(f => f.Attributes, f => f.CreationTime);
This example displays the following output to the console:
Attributes
CreationTime

Calling a Method using a string in another class that has already been instantiated

The problem I am having is that I would like to call a method from a string. Here is what I am doing:
Building My List of Strings (Methods) there are three different checkboxList objects in my UI
private List<string> MyTest = new List<string>();
private void AddSelectedMethods()
{
foreach(XName item in BaseTestList.CheckedItems)
{
MyTests.Add(item.ToString());
}
foreach (XName item in AdminTestList.CheckedItems)
{
MyTests.Add(item.ToString());
}
foreach (XName item in SubscriberTestList.CheckedItems)
{
MyTests.Add(item.ToString());
}
}
Here is the Caller. If I take out the Reflections call and reference the method directly everything works but I don't want to get into crating a huge list of if else statements.
private void StartSiteTest(object sender, DoWorkEventArgs e)
{
if (!BackWorker1.CancellationPending)
{
if (SiteToTest == "estatesales.vintagesoftware.local" || SiteToTest == "localhost")
{
es = new EstateSaleTests(site, Sites.First(i => i.SiteUrl == SiteToTest), BasePath, SiteToTest, UseCurrentCompanies);
foreach (string test in MyTests)
{
// <<<!!!!!!!! ------ The next line returns null ------ !!!!!!!>>>
MethodInfo thisMethod = es.GetType().GetMethod(test);
thisMethod.Invoke(es, null);
}
}
}
}
Any help on what I am doing wrong would be greatly appreciated.
!!!----- EDIT -----!!!
I'm an idiot. I had the class set to the list of strings but I forgot to rename my Methods Sorry about that. Yes the Methods were public and they are accessible I just have to rename them to the correct names now.
The call you use seems pretty acceptable, imo.
The thing is that GetType().GetMethod() is able to recover only public methods.
See this MSDN link.
In order to access methods with different acessors use this GetMethod(string, BindingFlags) overload.
Hope this helps.
I notice this has already been well-answered, but the following might still be of use.
Sometimes it is hard to find the method using reflection. You're currently just searching for public instance methods only. What I usually do when finding the method through reflection appears rather hard, is using GetMethods() with different binding flags and check by hand whether the expected methods are there.
Note that when you specify binding flags, you must also specify BindingFlags.InvokeMethod | BindingFlags.Instance. In addition, consider the following:
If the method is an instance method, use BindingFlags.Static
If you don't know whether you have the caption right ("CalcRoot" is different than "calcRoot") than use BindingFlags.IgnoreCase
If you think the method is protected, internal, private or protected internal, use BindingFlags.NonPublic
If you are not sure whether you use a derived type, use BindingFlags.FlattenHierarchy
If you are uncertain whether what you are looking for is a property, field or method, use GetMembers instead.
You can combine all flags with | to search for everything. With a little bit trial and error you'll eventually find the set of binding params that you need.

How do I write a method that can output data from any class using generics?

I'm trying to write a custom method to populate a ListView control using Generics:
private void BindDataToListView(List<T> containerItems)
{
this.View = View.Details;
this.GridLines = true;
this.FullRowSelect = true;
if (this.Items.Count > 0)
this.Items.Clear();
this.BeginUpdate();
int i = 0;
foreach (T item in containerItems)
{
// do something
}
this.EndUpdate();
}
The parameter containerItems can have many items since I'm using generics. But I get stuck in the foreach loop. How do I access the values in containerItems?
Do I have to use reflection on each instance of T in the foreach loop? I think I do to retrieve the property name. But once I have the property name of the type T, how do I retrieve the value?
The most common way of doing this (with winforms) is via TypeDescriptor; this allow you to use things DataTable the same as classes; the "full" pattern is quite complex (and involves checking for IListSource, ITypedList, etc; however, the short version is; to get the available properties:
PropertyDescriptorCollection props = TypeDescriptor.GetProperties(typeof(T));
To get a named property:
PropertDescriptor prop = props[propName];
To get a value for an instance (sourceObject):
object val = prop.GetValue(sourceObject);
To render a value as a string (using the designated converter):
string s = prop.Converter.ConvertToString(val);
You could limit T to an interface, and use that interface in the iteration.
What does T represent ?
Like it is now, it is a generic type and it can be ... anything.
So, what I would do, is create an interface IListViewBindable or something like that. That interface could then have a method 'CreateListViewItem' for instance.
Then, I would change the method, so that a constraint is applied to your type-parameter T, saying that T should implement IListViewBindable, like this:
public void BindDataToListView<T>( List<T> containerItems ) where T : IListViewBindable
{}
In your BindDataToListView method, you could then do this:
foreach( T item in containerItems )
{
this.Items.Add (item.CreateListViewItem());
}
If the items in the list are of totally unconstrained type, then you can treat them as simply of type object. You call GetType() to get the type of the object. On that you can call GetProperties() to get an array of PropertyInfo objects. And on those you can call GetValue() to retrieve the value of the property.
If you already know the name of a property, just call GetProperty() to retrieve it:
string valueAsString = item.GetType().GetProperty("Something")
.GetValue(item, null).ToString();
I don't completely understand what you're asking, but I think that this will point you in the right direction. Please ask for clarification if it looks like it can help and it's unclear.
You can access a given property of an object using reflection via
object o;
PropertyInfo info = o.GetType().GetProperty().GetProperty("NameOfPropertyIWant");
and you can get the value via
object value = info.GetValue(o, null);
Now, if you're going to be accessing a property of the same name on objects of various types, you should consider adding an interface
public interface IHasThePropertyIWant {
object NameOfPropertyIWant { get; }
}
Then you can enforce this via
void BindDataToListView(List<T> containerItems) where T : IHasThePropertyIWant
and use it in the look like so
foreach (T item in containerItems) {
object o = item.NameOfPropertyIWant;
// do something with o
}
ObjectListView uses reflection to do exactly what you are trying to do: populate a ListView using reflection. You could save yourself a lot of trouble by using it. It has already solved all the tricky problems you are going to encounter on this path.
If you really, really want to do it yourself, Marc's answer is (of course) completely correct.

C# Get Delegate (Func) implementation from concrete class via reflection?

Sorry for the poor explaination of the problem
Totally rewriting question
I have the following method:
public TReturn FindCached<TSearch, TReturn>(Func<TSearch, TReturn> searchMethod)
where TSearch : ISearchSpecification
where TReturn : class
{
SearchSpecification spec = new GetConcreteSearchSpecification<TSearch>();
//insert magic here to get an attribute from the method on
//the spec class that searchMethod invokes on the line below
return searchMethod(spec);
}
So I have a delegate (searchMethod) and an object (spec) that I want to invoke the delegate on. I want to inspect the object (spec) to find a custom attribute on the method that searchMethod will call when invoked.
Hope this is clearer.
Thanks
Assuming you meant searchMethod to be a variable of type Func<TSearch, TReturn> and mySearchSpec as some implementation of ISearchSpecification<TSearch>, then you are basically asking how to get attributes on a class.
For this, use something like:
object[] attrs = typeof(mySearchSpec).GetCustomAttributes(false);
Assuming that the mySearchSpec type is public, otherwise you may need a different overload for GetCustomAttributes
Addendum:
Based on your revised question, to get the attributes on a method on the actual type of spec used:
Type t = spec.GetType();
MethodInfo m = t.GetMethod("nameOfMethodToBeCalledHere");
object[] attrs = m.GetCustomAttributes(false);
Again, note that you may need overloads for GetMethod or GetCustomAttributes depending on the implementation of the actual class.
Note:
It does seem however like you might be asking for the method called in return searchMethod(spec);, but that is searchMethod and not some method on spec at all.
If you want attributes on searchMethod (nothing to do with spec):
MethodInfo m = searchMethod.Method;
object[] attrs = m.GetCustomAttributes(false);
I think that now covers all permutations of meaning...
This is quite a confusing question, let's see if I have it right:
You have a lambda function (which you describe as a delegate) called searchMethod.
You have a factory-pattern generated object called spec
So you have a method somewhere like this:
[MyCustomAttribute]
public RetClass MyMethod( SearchSpecification input ) {
return input.GetRetClass();
}
And you call this method with:
var result = FindCached( MyMethod );
And in FindCached you want to find MyCustomAttribute - in that case #jerryjvl's answer is right.
Your problem is that you could also do:
var result = FindCached( x => x.GetRetClass() );
I'm not sure from your description whether it's an attribute on the x.GetRetClass() that you actually want. In this case you need to take the lambda apart using expressions, but I wouldn't recommend it - a more complex lambda declaration will result in an anonymous delegate, which is a black-box when you try to parse it at run time.
Instead, as you're using reflection anyway, it might be easier to pass the name of the method that you want instead of the delegate reference:
var result = FindCached( "GetRetClass" );
I ran into a similar situation, jerryjvl's answer explained exactly what I wanted.
I wanted to create a generic profiling method, where I could time how long some method took to run, and retrieve the name of the method using reflection for logging purposes.
The MethodInfo was the key.
Where I have a method like:
public static bool TimeMethod(Func<bool> someMethod)
And then later I want to dynamically get it's name or some attributes off it.
MethodInfo m = someMethod.Method;
object[] attrs = m.GetCustomAttributes(typeof(MonitoringDescriptionAttribute), true);
string name = m.Name;
Cheers

Categories

Resources