Let’s say I have a class that looks like this:
public class CallByAttribute
{
[CodeName("Foo")]
public string MyProperty { get; set; }
[CodeName("Bar")]
public string MyMethod(int someParameter)
{
return myDictionary[someParameter];
}
}
How would I call these two properties or methods, using CodeName instead of the property or method name?
Method 1:
Note: This will throw if no property exists with the specified
codeNameor if multiple properties share the samecodeName.Usage:
Method 2:
If you are on C# 4, you can write your own
System.Dynamic.DynamicObjectthat can route dynamic calls to the right member.This will allow for cleaner syntax. For example, you should be able to accomplish something that allows: