Creating dynamic variable names in C#

前端 未结 4 1358
北海茫月
北海茫月 2020-12-17 03:25

I am trying to write a simple role playing game in C# to become more familiar with the language.

I have a class that loads data from CSV file, creates an object, and

4条回答
  •  南方客
    南方客 (楼主)
    2020-12-17 03:57

    Derive from DynamicObject and override TryGetMember so that it returns the appropriate dictionary entry. Exactly what Microsoft did in MVC 3's ViewBag. Example usage (if your derived type was named CsvBag):

    dynamic bag = new CsvBag(csvFileStream);
    _monitor.Monster.LookUp(*bag.Id*).Attack(player); // whatever.. 
    

提交回复
热议问题