Verify Function Signature at compile time using System.Attributes

Is there a way to verify a function signature at compile time in C#
What I want to do is invoke functions with agreed signature at runtime. Can System.Attributes be used for this purpose How should I proceed

P


Answer this question

Verify Function Signature at compile time using System.Attributes

  • VarunK

    It sounds to me like you're really try to invoke an arbitrary method rather then ensuring that a function's signature is valid. Otherwise the reflection approach would be sufficient. You could use the Invoke method on MethodInfo and pass an array of objects to use as parameters. That works for both instance and static methods. And it would allow functions defined in an XML document to be invoked.



  • GhostWalker 28

    Thanks for all your replies.
    Point#1 is what I actually implemented before I started this thread. But what I wanted was an alternative solution.
    I am reading the function list from an xml file.
    Interface sounds good and (was/still is) my SOS.

    But I was just trying to give the users of my library full freedom of implementation meaning the function they define may or maynot require any instance and stuff like that.
    Regarding delegates, Delegates are already there and are working fine when used in a code. But the problem arises from the functions defined in the Configuration Xml.

    Anyway I think only throw and debug assertion will teach the (mis)user a lesson...
    Thanks

    --P

  • BrianCogs887

    I can think of three possible approaches to what you’re trying to do, depending on the specifics.

    1. Use reflection to retrieve the MethodInfo for the method that you want to call and check the parameter list.
    2. Have all of the classes that you might want to call implement an interface that contains a function with the appropriate signature. You could then check to see if a class implements the interface and, if so, invoke it.
    3. Create a delegate with the appropriate signature and then use delegate assignment to point at the desired method, catching an exception if there’s a problem.

    If you want to fill us in on the details, we might be able to come up with other/better choices.

    Bruce Johnson [C# MVP]
    http://www.objectsharp.com/blogs/bruce


  • Terence Curd

    pardgr8 wrote:
    Is there a way to verify a function signature at compile time in C#
    What I want to do is invoke functions with agreed signature at runtime. Can System.Attributes be used for this purpose How should I proceed

    No you can't use attributes for this. You would need to modify a C# compiler to do it, or write a tool that can run as a post compilation step.



  • Verify Function Signature at compile time using System.Attributes