Last active
June 30, 2025 22:29
-
-
Save amirrajan/302fc317478122261bf9a6e2c40a7634 to your computer and use it in GitHub Desktop.
C# method missing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Person : Gemini | |
{ | |
//define a method called MethodMissing taking in one parameter | |
dynamic MethodMissing(dynamic callInfo) | |
{ | |
//check to see if the method being called ends in Html | |
if(callInfo.Name.EndsWith("Html")) | |
{ | |
//if it does, add a method on the fly that returns | |
//the html encoded version of the parsed property | |
var member = callInfo.Name.Replace("Html", ""); | |
SetMember(callInfo.Name, | |
new DynamicFunction(() => | |
{ | |
return System.Web.HttpUtility.HtmlEncode(GetMember(member)); | |
}); | |
//return the value of the method | |
return GetMember(callInfo.Name)(); | |
} | |
//if the pattern isn't there, throw an exception defined in Gemini | |
throw (Exception)MemberDoesntExistException(callInfo.Name); | |
} | |
} | |
dynamic person = new Person(); | |
person.FirstName = "Jane<>"; //set the FirstName Property | |
person.FirstName; //this would return Jane<> | |
/* | |
attempt to call FirstNameHtml() | |
calling this the first time will execute the | |
method missing block with the following callInfo | |
this (Gemini) | |
Name (String): FirstNameHtml | |
Parameters (IEnumerable<dynamic>) | |
ParameterNames (IEnumerable<dynamic>) | |
Instance (Person) | |
MethodMissing (DynamicFunctionWithParam) | |
SetMembers (DynamicFunctionWithParam) | |
FirstName (String): Jane<> | |
Parameter (Gemini) | |
SetMembers (DynamicFunctionWithParam) | |
SetMembers (DynamicFunctionWithParam) | |
*/ | |
//this would print Jane<> | |
Console.WriteLine(person.FirstNameHtml()); | |
//method missing will not get called again because now the method exists :-) | |
//this would print Jane<> | |
Console.WriteLine(person.FirstNameHtml()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment