Skip to content
Permalink
Browse files

Game - Noun.Register using reflection

  • Loading branch information...
bobbymcr committed Dec 19, 2018
1 parent 90ad629 commit dcd68c6ac62c0ecf39b05709e07f5606ae2a09c0
Showing with 11 additions and 4 deletions.
  1. +1 −4 sample/src/Game.cs
  2. +10 −0 sample/src/Noun.cs
@@ -43,10 +43,7 @@ private static Words InitializeWords()
w.Add(Verb.Take, "get");
w.Add(Verb.Inventory, "inv");

w.Add(Noun.Coin);
w.Add(Noun.East);
w.Add(Noun.Table);
w.Add(Noun.West);
Noun.Register(w);

return w;
}
@@ -4,6 +4,8 @@

namespace Adventure.Sample
{
using System.Reflection;

internal sealed class Noun
{
public static readonly Noun Coin = new Noun("coin");
@@ -19,5 +21,13 @@ private Noun(string noun)
}

public static implicit operator string(Noun n) => n.noun;

public static void Register(Words words)
{
foreach (FieldInfo fi in typeof(Noun).GetFields(BindingFlags.Static | BindingFlags.Public))
{
words.Add((Noun)fi.GetValue(null));
}
}
}
}

0 comments on commit dcd68c6

Please sign in to comment.
You can’t perform that action at this time.