1

これは単純なことかもしれませんが、私の頭はそれを包み込むことを拒否しているので、その場合は常に外の景色が役に立ちます!

患者のパラメータ登録を実装するために、オブジェクト階層を設計する必要があります。これは特定の日に行われ、患者に関するさまざまなパラメータ(血圧、心拍数など)を収集します。これらのパラメーター登録の値は、文字列、整数、浮動小数点数、さらにはGUID(ルックアップリストの場合)など、さまざまなタイプにすることができます。

だから私たちは持っています:

public class ParameterRegistration
{
  public DateTime RegistrationDate { get; set; }
  public IList<ParameterRegistrationValue> ParameterRegistrationValues { get; set; }
}

public class ParameterRegistrationValue
{
  public Parameter Parameter { get; set; }
  public RegistrationValue RegistrationValue { get; set; }   // this needs to accomodate the different possible types of registrations!
}


 public class Parameter
  {
    // some general information about Parameters
  }

public class RegistrationValue<T>
{
  public RegistrationValue(T value)
  {
    Value = value;
  }

  public T Value { get; private set; }
}

更新:提案のおかげで、モデルは次のようにモーフィングされました。

public class ParameterRegistration
{
  public DateTime RegistrationDate { get; set; }
  public IList<ParameterRegistrationValue> ParameterRegistrationValues { get; set; }
}

public abstract class ParameterRegistrationValue() 
{
  public static ParameterRegistrationValue CreateParameterRegistrationValue(ParameterType type)
{
    switch(type)
    {
        case ParameterType.Integer:
            return new ParameterRegistrationValue<Int32>();
        case ParameterType.String:
                return new ParameterRegistrationValue<String>();
        case ParameterType.Guid:
            return new ParameterRegistrationValue<Guid>();
        default: throw new ArgumentOutOfRangeException("Invalid ParameterType: " + type);
    }
}
  public Parameter Parameter { get; set; }
}

public class ParameterRegistrationValue<T> : ParameterRegistrationValue
{
  public T RegistrationValue {get; set; }
}

public enum ParameterType
{
  Integer,
  Guid,
  String
}

public class Parameter
{
  public string ParameterName { get; set; }
  public ParameterType ParameterType { get; set;}
}

これは確かに少し簡単ですが、ParameterRegistrationのIListが抽象ParameterRegistrationValueオブジェクトを指しているので、実際の値を(サブオブジェクトに格納されているため)どのように取得できるのでしょうか。

たぶん、一般的なこと全体は、結局のところ完全に進む方法ではありません:s

4

4 に答える 4

4

パラメータの最終セットと各パラメータの対応するタイプがわからない場合は、ジェネリックはおそらく役に立ちません-パラメータ値タイプとしてオブジェクトを使用してください。

さらに、値の処理方法を決定するために各項目のタイプを調べる必要があるため、パラメーターのリストを反復処理するのは面倒です。

ジェネリックで何を達成しようとしていますか?はい、それらはかっこいいです(そして、ボクシング/アンボクシングに行くのはおそらく最良のアイデアではありません)が、場合によっては、代わりにオブジェクトを使用したい場合があります(単純さと柔軟性の両方のために)。

-パベル

于 2011-01-04T10:07:26.490 に答える
2

導入したいのは、RegistrationValue<T>ジェネリックではない抽象基本クラスです。これParameterRegistrationValueにより、関連する型の知識がなくても、クラスは非ジェネリック参照を保持できます。ParameterRegistrationValueあるいは、ジェネリックも作成し、代わりに非ジェネリック基本クラスを追加することが適切な場合があります(そのため、の値のリストParameterRegistrationは異なるタイプにすることができます。

1番目の方法:

public abstract class RegistrationValue
{
}

public class RegistrationValue<T> : RegistrationValue
{
  public RegistrationValue(T value)
  {
    Value = value;
  }

  public T Value { get; private set; }
}

これで、コードがコンパイルされます。


非ジェネリック基本クラスができたら、ジェネリック型パラメーターに依存しないジェネリッククラスのメンバーもこの基本クラスに移動します。この例には何もありませんが、代わりParameterRegistrationValueにジェネリックに変更する場合はParameter、非ジェネリック基本クラスに移動します(のtypeパラメーターに依存しないためRegistrationValue

于 2011-01-04T09:58:32.913 に答える
0

おそらく、public RegistrationValue RegistrationValueを使用する必要があります。ここで、T-はタイプであり、genericで使用します。たとえば、T-はStringまたは他のクラスまたは構造体です。

または、RegistrationValueフィールドでジェネリック引数を使用するには、クラスParameterRegistrationValueをジェネリックとして作成する必要があります。

于 2011-01-04T09:48:04.793 に答える
0

さまざまなs派生クラスのインスタンスのコレクションが必要でRegistrationValueあり、それを反復できるようにし、要素ごとに異なるタイプを使用できるようにする必要があると思います。それはかなり不可能です。

コレクションを反復処理すると基本型( -typeパラメーターParameterRegistrationValueで指定されたもの)への参照が返されるため、各要素を既知の型にキャストする必要があります。したがって、非ジェネリックリストIListを反復処理することと実際の違いはありません。object

そして、パラメーターごとにそのキャストを安全に行うことができれば(すべてのタイプを知っている)、おそらくこのようなコレクションはまったく必要ありません-すべてのパラメーターを1つのタイプにカプセル化するクラスがある方が良いでしょう。次のように、IntelliSenseなどの強力な型で呼び出すことができます。

public class ParameterRegistration
{
  public DateTime RegistrationDate { get; set; }
  public PatientData PatientData { get; set; }
  public Guid Identifier { get; set; }
  // ...
}
于 2011-01-04T10:14:26.120 に答える