私は2つのクラスを持っています:
public class Variable<T>;
public class Closure;
どちらも次のプロパティを共有しています。
public string handle;
public string description;
どちらにも次の名前のメソッドがありGetValue
ます:
public T GetValue(); // Variable<T>
public string GetValue(params string[] arguments); // Closure
Variable<T>
追加のメソッドがありSetValue
ます:
public string SetValue(object newValue);
これらのクラスは、ビデオ ゲーム、コンソール コンポーネント プロパティを表します。
私がやりたいのは、これらの両方を 1 つの内に保持しDirectory
ながら、パブリック プロパティ、クラスのメソッドに簡単にアクセス/操作できるようにすることです。
ダミーを追加しようとしましたinterface
が、オブジェクトとの関係が失われ、インターフェイス インスタンスが返されたため、これらのパブリック プロパティ、メソッドを使用できなくなりました。
public static class Storage
{
public static Dictionary<string, IConsoleProperty> Variables = new Dictionary<string, IConsoleProperty>();
public static string Inform()
{
string output = "";
foreach (var variable in Variables)
{
output += string.Format("{0} : {1}", variable.Key, variable.Value.description);
}
return output;
}
}
型
Console.IConsoleProperty
に定義が含まれておらず、description
拡張メソッドdescription' of type
Console.IConsoleProperty` が見つかりませんでした (using ディレクティブまたはアセンブリ参照がありませんか?)
typeof(variable.Value)
そのようなシナリオでキャストする必要があることを読みましたが、特にGeneric
複数の型のインスタンスでstring () から動的にキャストする方法がわかりません。
これらのクラスの両方を 1 つのディレクトリ内に保持し、値の取得時にインターフェイスではなく基本クラスのインスタンスを取得する方法は?