0

私は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 typeConsole.IConsoleProperty` が見つかりませんでした (using ディレクティブまたはアセンブリ参照がありませんか?)

typeof(variable.Value)そのようなシナリオでキャストする必要があることを読みましたが、特にGeneric複数の型のインスタンスでstring () から動的にキャストする方法がわかりません。

これらのクラスの両方を 1 つのディレクトリ内に保持し、値の取得時にインターフェイスではなく基本クラスのインスタンスを取得する方法は?

4

2 に答える 2