3

スタック オーバーフローに投稿するのはこれが初めてです。

クラスの型だけが与えられたときに、クラスに属する静的変数にアクセスする方法が C# にあるかどうか疑問に思っています。例えば:

public class Foo
{
    public static int bar = 0;
}

public class Main
{
    public void myFunc(Type givenType)
    {
        int tempInt = ??? // Get the value of the variable "bar" from "Foo"
        Debug.WriteLine("Bar is currently :" + tempInt);
    }
}

// I didn't run this code through a compiler, but its simple enough
// that hopefully you should get the idea...

これを知る必要がある状況を説明するのは難しいですが、私は XNA でゲームを作成しており、参照カウントを使用して設計の複雑さを軽減しようとしています。ゲーム内のオブジェクトと、それらに効果を適用できるパワーアップがあります (オブジェクトにとどまります)。パワーアップは消滅する可能性がありますが、その効果はオブジェクトに残っている可能性があります。パワーアップによる効果がまだオブジェクトに残っているかどうかを追跡する必要があります (したがって、参照カウント)。影響を受けるオブジェクトの数を保存する静的な整数を持つ "PowerUpEffect" クラス (パワーアップの各タイプ用) を作成する予定ですが、PowerUpEffect を渡すとゲームの残りの部分の設計がうまく機能しません。 PowerUpEffect クラスのメソッドを呼び出すためのオブジェクトに至るまで。

PowerUpEffect の型 (「typeOf()」のようなものを使用) のみを渡し、その型を使用してそれらの型に属する静的変数を参照することを望んでいますが、それを行う方法やそれが可能かどうかさえわかりません。

この質問に直接答えるのではなく、シンプルでエレガントなデザインで問題を解決する回避策を見つけていただければ幸いです。=)

ヘルプ!(ありがとう!)

4

6 に答える 6

5

Type ハンドルしかない場合は、次のようにすることができます。

var prop = givenType.GetProperty("bar");
var value = prop.GetValue(null);
于 2012-05-31T16:58:26.423 に答える
2

これはDictionaryおそらく、ある値のセットを別の値にマッピングする最も簡潔な方法です。int値をs に関連付ける場合はType、次のようにします。

public static readonly Dictionary<Type, int> sTypeValues =
   new Dictionary<Type, int>
{
   { typeof(Type1), 5 },
   { typeof(Type2), 10 },
   { typeof(Type3), 2 },
   { typeof(Type4), 3 },
   { typeof(Type5), -7 }
};

関数は次のようになります。

public void myFunc(Type givenType)
{
    int tempInt = sTypeValues[givenType];
    Debug.WriteLine("Bar is currently :" + tempInt);
}
于 2012-06-01T05:48:29.287 に答える
1

int tempInt = (int) givenType.GetField("バー").GetValue(null);

于 2012-05-31T17:00:00.160 に答える
0

アクセスしようとしているフィールドの名前 (たとえば、クラス「foo」、フィールド「bar」) が Type パラメーターに基づく別のフィールドであるかどうかを想定していますか?

フィールドの名前が許容可能な型の有限数に基づいてわかっている場合は、switch ステートメントでそれを決定できるはずです。例えば:

public class Foo
{
    public static int bar = 0;
}

public class Baz
{
    public static int bing = 0;
}

public class Main
{
    public void myFunc(Type givenType)
    {
        switch (givenType.ToString())
        {
            case "Foo":
                Debug.WriteLine("Bar is currently :" + Foo.bar);
                break;
            case "Baz":
                Debug.WriteLine("Bing is currently :" + Baz.bing);
                break;
        }
    }
}
于 2012-05-31T17:11:52.717 に答える
0

あなたが正しいと思っているなら、これはあなたにいくつかのアイデアを与えるかもしれません:

システムを使用する; System.Reflection を使用します。

public class RStatic
{
    private static int SomeNumber {get; set;}
    public static object SomeReference {get; set;}
    static RStatic()
    {
        SomeReference = new object();
        Console.WriteLine(SomeReference.GetHashCode());
    }
}

public class Program
{
    public static void Main()
    {
        var rs = new RStatic();
        var pi = rs.GetType().GetProperty("SomeReference",  BindingFlags.Static | BindingFlags.Public); // i have used GetProperty in my case

        Console.WriteLine(pi.GetValue(rs, null).GetHashCode());


    }
}
于 2012-05-31T16:55:58.647 に答える
0

さて、パワーアップのコレクションがあり、それらのパワーアップのそれぞれに整数を関連付けたいとします。それぞれが静的な整数を持つ多くのクラスを持つのではなく、すべてのパワーアップとそれに関連する整数値を保持する単一の静的コレクションを持つことができます。

public static class MyPowerupInfo
{
  public static Dictionary<PowerUp, int> PowerUps {get; private set;}
  static MyPowerupInfo
  {
    PowerUps = new Dictionary<PowerUp, int>();
    PowerUps.Add(*some power up object goes here*, 0);
    //TODO add other power ups
  }
}

次に、それを使用するには、次のようなことができます。

int powerupCount = MyPowerupInfo.PowerUps[wickedAwesomePowerup];

また:

public static void IncrementPowerup(Powerup powerup)
{
  MyPowerupInfo.PowerUps[powerup] = MyPowerupInfo.PowerUps[powerup]+1;
}
于 2012-05-31T17:00:38.253 に答える