1

作成した明示的なインターフェイスに問題があり、例外が発生しています。

'x' には 'y' の定義が含まれておらず、タイプ 'x' の最初の引数を受け入れる拡張メソッド 'y' が見つかりませんでした

私は一連のクラスを持っています。基本クラス:

public interface IFactoryResponse
{
    object instance { get; set; }
    string instanceconfig { get; set; }
}

それを明示的に実装するクラス:

public class FactoryResponseImpl : IFactoryResponse
{
    object IFactoryResponse.instance {
        get { return ((IFactoryResponse)this).instance; }
        set { ((IFactoryResponse)this).instance = value; }
    }

    string IFactoryResponse.instanceconfig   {
        get { return ((IFactoryResponse)this).instanceconfig; }
        set { ((IFactoryResponse)this).instanceconfig = value; }
    }
}

別のクラスでは、上記のエラーが発生します。Visual Studio はインターフェイスとクラスを正常に検出できますが、インスタンス プロパティを解決できません。ここで何が欠けていますか。明示的な継承のより洗練されたルールの 1 つが欠けている可能性があります。

if (facconfig.useabstract) {
    response.instance = Activator.CreateInstance(m_entassembly.GetType(entconfig.Classname, true, true));
    response.instanceconfig = facconfig.config;
} else {
    Assembly assem = Assembly.LoadFrom(facconfig.assemblyfile);
    object Obj = Activator.CreateInstance(assem.GetType(facconfig.Classname, true, true));
    response.instance = Obj;
    response.instanceconfig = facconfig.config;
}
4

4 に答える 4

2
  1. あなたの実装は正しくありません。StackOverflowExceptionプロパティが自分自身を呼び出すために発生します。autoproperties を使用して、プロパティを簡単に実装できます。

    public class FactoryResponseImpl : IFactoryResponse
    {
        object IFactoryResponse.instance { get; set; }
    
        string IFactoryResponse.instanceconfig { get; set; }
    }
    
  2. インターフェイスメンバーが明示的に実装されている場合、クラスインスタンスをそのインターフェイスにキャストするか、そのインターフェイスとして変数型に割り当てることにより、変数をインターフェイスとして見る必要があります。

    if (facconfig.useabstract) {
        ((IFactoryResponse)response).instance = Activator.CreateInstance(m_entassembly.GetType(entconfig.Classname, true, true));
        ((IFactoryResponse)response).instanceconfig = facconfig.config;
    } else {
        Assembly assem = Assembly.LoadFrom(facconfig.assemblyfile);
        object Obj = Activator.CreateInstance(assem.GetType(facconfig.Classname, true, true));
        ((IFactoryResponse)response).instance = Obj;
        ((IFactoryResponse)response).instanceconfig = facconfig.config;
    }
    
  3. インターフェイスを明示的に実装する必要があるのはなぜですか? よほどの理由がない限り、そうすべきではありません。暗黙的な実装を使用すると、すべてがはるかに簡単になります。

    public class FactoryResponseImpl : IFactoryResponse
    {
        public object instance { get; set; }
    
        public string instanceconfig { get; set; }
    }
    

    そして、他のコードは問題なく動作するはずです。

于 2014-01-26T18:19:22.417 に答える
0

これはあなたが目指しているものですか?

public interface IFactoryResponse
        {
            object instance { get; set; }
            string instanceconfig { get; set; }
        }

        public class FactoryResponseImpl : IFactoryResponse
        {
            object IFactoryResponse.instance { get; set; }
            string IFactoryResponse.instanceconfig { get; set; }
        }

        class Test
        {
            public void TestMethod()
            {

                IFactoryResponse response = new FactoryResponseImpl();
                response.instance = null;
            } 
        }
于 2014-01-26T18:19:49.403 に答える
0

明示的な実装は自分自身を参照しています。プライベート フィールドまたはパブリック実装を参照する必要があります。例えば:

public class FactoryResponseImpl : IFactoryResponse
{
    DatabaseFactoryResponseInstance _instance;

    public FactoryResponseImpl()
    {
        _instance = new DatabaseFactoryResponseInstance();
    }

    object IFactoryResponse.instance {
    get { return (object)_instance; }
    set { 
            if (value != null)
            {
                DatabaseFactoryResponseInstance dbInstance;
                dbInstance = value as DatabaseFactoryResponseInstance;
                if (dbInstance == null)
                    throw new InvalidOperationException();

                _instance = dbInstance;
            }
         }
}
于 2014-01-26T18:19:05.847 に答える
0

IFactoryResponse.instance のような明示的なインターフェイス実装を使用する場合、これらのメソッドは公開されません。IFactoryResponse にキャストしてアクセスするか、メソッドを public: public object instance { ... } として定義する必要があります。

于 2014-01-26T18:20:34.580 に答える