dimanche 24 septembre 2017

How to determine the method signature in C# reflection with list of object parameters

I have a C# reflection question that I'm not sure how to achieve.

I need to allow access to string methods in an XML configuration file for a product I'm working on.

For example:

<someElement field="someField.Substring(5,10)">

I can gather the method - Substring - and the parameters - 5 and 10 easily enough.

If there is an overloaded method with the same number of parameters, but of different types, how will I know which one to use when I can't be sure of the input parameter types?

At the moment I can try to convert the parameters to the types in the signature and if they pass then perform that method. If they fail the conversion then I know for sure not to use that method. If two methods succeed then I'm lost.

The other option is to force in the configuration file the user to put the type before the parameter like:

<someElement field="someField.Substring(int 5, int 10)">

This is what I think I'll have to do unless I'm missing some aspect of reflection that would make this easier (I don't have much experience using reflection).





Aucun commentaire:

Enregistrer un commentaire