0

多くのリモート データベースからデータを収集し、それをマスター データベースにコンパイルするサービスを開発しています。2 つのデータベース間で共通のデータを含むインターフェイスがあります。インターフェイスは、モデルとビューモデルの間のリンクとしても機能します。

RemoteDatabase のインスタンスからデータを取得し、そのすべてを MasterDatabase のインスタンスに入れたいと考えています。

public interface IInterface
{
    //Common interface properties in both databases
    long PK { get; set; }
    Nullable<long> RUN_ID { get; set; }
    string Recipe_Name { get; set; }
    string Notes { get; set; }
    //Lots more properties from a database
}

class RemoteDatabase : IInterface
{
    //Common interface properties in both databases
    public long PK { get; set; }
    public Nullable<long> RUN_ID { get; set; }
    public string Recipe_Name { get; set; }
    public string Notes { get; set; }
    //Lots more properties from a database
}

class MasterDatabase : IInterface
{
    //Additional properties that Remote Database doesn't have
    public int locationFK { get; set; }

    //Common interface properties from database
    public long PK { get; set; }
    public Nullable<long> RUN_ID { get; set; }
    public string Recipe_Name { get; set; }
    public string Notes { get; set; }
    //Lots more properties from a database

    public MasterDatabase(IInterface iInterface)
    {
        var interfaceProps = typeof(IInterface).GetProperties(BindingFlags.Public | BindingFlags.Instance);

        foreach (PropertyInfo p in interfaceProps)
        {
            p.Name;
        }
    }
}

私はオブジェクトをキャストしようとしましたが、無効なキャスト例外が発生しました.しかし、私が欲しいのは、IAnimal で定義された共通のプロパティを取得することです)。

キャストできなかったので、リフレクションを使用して、インターフェイスが更新された場合に MasterDatabase クラスのすべての新しいオブジェクトが自動的に更新されるようにしたいと考えています。リフレクションを使用してインターフェイスからすべてのプロパティを取得しましたが、propertyInfo.name を使用して MasterDatabase クラスの値を取得するにはどうすればよいでしょうか。

おそらく、明らかな何かが欠けているか、これを行うためのはるかに良い方法があります。助けや提案をいただければ幸いです。

前もって感謝します。

4

1 に答える 1

1

ターゲットとしてSetValue使用して、PropertyInfo オブジェクトを呼び出す必要があります。渡す値は、コンストラクター パラメーターで対応する呼び出しthisを使用して取得する必要があります。GetValue

foreach (PropertyInfo p in interfaceProps)
{
    p.SetValue(this, p.GetValue(iInterface, null), null);
}
于 2013-10-16T03:31:39.517 に答える