You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
List of customers and suppliers
Supplier: Lucerne Publishing (https://www.lucernepublishing.com/)
Customer: Preston, Chris
Customer: Hines, Patrick
Customer: Cameron, Maria
Supplier: Graphic Design Institute (https://www.graphicdesigninstitute.com/)
Supplier: Fabrikam, Inc. (https://www.fabrikam.com/)
Customer: Seubert, Roxanne
Supplier: Proseware, Inc. (http://www.proseware.com/)
Customer: Adolphi, Stephan
Customer: Koch, Paul
To create a sample application that uses the custom dynamic object
dynamicrFile=new ReadOnlyFile(@"..\..\..\TextFile1.txt");foreach(string line in rFile.Customer){
Console.WriteLine(line);}
Console.WriteLine("----------------------------");foreach(string line in rFile.Customer(StringSearchOption.Contains,true)){
Console.WriteLine(line);}
Create a Custom Dynamic Object
publicenumStringSearchOption{StartsWith,Contains,EndsWith}internalclassReadOnlyFile:DynamicObject{// Store the path to the file and the initial line count value.privatestringp_filePath;// Public constructor. Verify that file exists and store the path in// the private variable.publicReadOnlyFile(stringfilePath){if(!File.Exists(filePath)){thrownew Exception("File path does not exist.");}p_filePath=filePath;}publicList<string>GetPropertyValue(stringpropertyName,StringSearchOptionStringSearchOption= StringSearchOption.StartsWith,booltrimSpaces=true){StreamReader?sr=null;List<string>results=new();stringline;stringtestLine;try{sr=new StreamReader(p_filePath);while(!sr.EndOfStream){line= sr.ReadLine();// Perform a case-insensitive search by using the specified search options.testLine= line.ToUpper();if(trimSpaces){testLine= testLine.Trim();}switch(StringSearchOption){case StringSearchOption.StartsWith:if(testLine.StartsWith(propertyName.ToUpper())){ results.Add(line);}break;case StringSearchOption.Contains:if(testLine.Contains(propertyName.ToUpper())){ results.Add(line);}break;case StringSearchOption.EndsWith:if(testLine.EndsWith(propertyName.ToUpper())){ results.Add(line);}break;}}}catch{// Trap any exception that occurs in reading the file and return null.results=null;}finally{if(sr!=null){ sr.Close();}}returnresults;}// Implement the TryGetMember method of the DynamicObject class for dynamic member calls.publicoverrideboolTryGetMember(GetMemberBinderbinder,outobjectresult){result= GetPropertyValue(binder.Name);returnresult==null?false:true;}// Implement the TryInvokeMember method of the DynamicObject class for// dynamic member calls that have arguments.publicoverrideboolTryInvokeMember(InvokeMemberBinderbinder,object[]args,outobjectresult){StringSearchOptionStringSearchOption= StringSearchOption.StartsWith;booltrimSpaces=true;try{if(args.Length >0){StringSearchOption=(StringSearchOption)args[0];}}catch{thrownew ArgumentException("StringSearchOption argument must be a StringSearchOption enum value.");}try{if(args.Length >1){trimSpaces=(bool)args[1];}}catch{thrownew ArgumentException("trimSpaces argument must be a Boolean value.");}result= GetPropertyValue(binder.Name, StringSearchOption, trimSpaces);returnresult==null?false:true;}}