質問を再入力しました。下にスクロールして元に戻すプロパティが汎用プロパティの通知を変更しました
[OK]を、多分私はあなたに全体の画像を与えていたはずです。などのトランスフォームと呼ばれるクラス、メッシュ、衝突、外観は、Component
から派生したすべてとすべてが上記のように宣言されているプロパティが、何もないもあります
public class Movement : Component
{
private Vector3 linearVelocity;
public Vector3 LinearVelocity
{
get
{
return linearVelocity;
}
set
{
if (value != linearVelocity)
{
linearVelocity = value;
ComponentChangedEvent<Movement>.Invoke(this, "LinearVelocity");
}
}
}
// other properties (e.g. AngularVelocity), which are declared exactly
// the same way as above
}
:私はこのようになり、多くのクラスを持っています。ここで重要なことは、ComponentChangedEvent
を呼び出すことです。すべてが完璧に機能しますが、私はそれぞれのプロパティに対して同じロジックを何度も何度も書き直す必要がない方法を探していました。
私は外観がhereであり、一般的なプロパティを使用するアイデアが好きでした。
public class ComponentProperty<TValue, TOwner>
{
private TValue _value;
public TValue Value
{
get
{
return _value;
}
set
{
if (!EqualityComparer<TValue>.Default.Equals(_value, value))
{
_value = value;
ComponentChangedEvent<TOwner>.Invoke(
/*get instance of the class which declares value (e.g. Movement instance)*/,
/*get name of property where value comes from (e.g. "LinearVelocity") */);
}
}
}
public static implicit operator TValue(ComponentProperty<TValue, TOwner> value)
{
return value.Value;
}
public static implicit operator ComponentProperty<TValue, TOwner>(TValue value)
{
return new ComponentProperty<TValue, TOwner> { Value = value };
}
}
それから私はこのようにそれを使用します:
public class Movement : Component
{
public ComponentProperty<Vector3, Movement> LinearVelocity { get; set; }
public ComponentProperty<Vector3, Movement> AngularVelocity { get; set; }
}
しかし、私は線速度がから来ても、それは文字列として名前だインスタンスを取得することはできませんよ、私はこのようになります思い付きました。だから私の質問は、もしこれが全て可能ならば...
しかし、それは私が行っていたやり方とは別の選択肢がないように思えます。
元の質問:私はこのような何かを持っている別の文脈では
public class Foo
{
public int Bar { get; set; }
}
:
プロパティ
から宣言クラスのインスタンスを取得します。私は、プロパティを持つクラスを持っています:
Foo fooInstance = new Foo();
DoSomething(fooInstance.Bar);
次に、DoSomething
でparameter
以外何も持っていないことをfooInstance
から取得する必要があります。文脈からは、整数がDoSomething
に渡されるのではなく、intのパブリックプロパティのみが渡されると仮定するのが節約されます。
public void DoSomething(int parameter)
{
// need to use fooInstance here as well,
// and no, it is not possible to just pass it in as another parameter
}
これはまったく可能ですか?リフレクションを使用するか、おそらくプロパティのカスタム属性Bar
を使用しますか?
デザインを再考する必要があります。これは不可能です。 – BrokenGlass
私はあなたが求めているものを得られない...なぜあなたは議論として 'フー'を与えることができないのですか? – log0
なぜ別のパラメータとして渡すことができないのですか? –