Is there anyway i could get a uniqueId for every method in a specific class using reflection based on its signature? I am aware of the GetHashCode method, but I want guaranteed uniqueness.
3 Answers
Type.FullName + MemberInfo.Name + argument types for a method, anything shorter will not be unique.
If you want to scope it for particular type - no need for type's FullName. If you need it only at run-time - MemberInfo objects themselves are probably the best thing.

- 98,904
- 14
- 127
- 179
-
i was hoping to get a readable number, that would get me a very long string. – Ahmed Galal Jun 25 '12 at 17:08
-
2To get the unique method signature you also need to factor in number of type parameters and the method of passing each formal parameter. See http://stackoverflow.com/a/8809191/1382376. – 0b101010 Apr 14 '15 at 18:52
How unique does it have to be? What size?
If you just concatenate the return type and every argument type using commas, you have a string that uniquely identifies this signature. It also happens to fully encode the signature, but that's not necessarily bad.
If you want something shorter, you could:
- hash it using a cryptographic hash function. Slow, the hash is long, but extremely likely to be unique
- hash it using a simple hash function. Collisions might occur, but the Id is shorter.
- store them in some sort of a lookup table or database. Your IDs are then just sequential integers.

- 59,298
- 38
- 251
- 324
-
i thought i could find an already builtin property or method in the MethodInfo class that has it, but i'll take urs as an answer – Ahmed Galal Jun 25 '12 at 17:05
Using the System.Reflection Namespace:
http://msdn.microsoft.com/en-us/library/system.reflection.aspx
Name of Method:
System.Reflection.MethodBase.GetCurrentMethod().Name
Type of method:
System.Reflection.MethodBase.GetCurrentMethod().DeclaringType
Parameters of the method:
System.Reflection.MethodBase.GetCurrentMethod().GetParameters().

- 1,030
- 11
- 7