Your implementation is incorrect. It will cause
StackOverflowException
because property calls itself. You can easily implement the properties using autoproperties:public class FactoryResponseImpl : IFactoryResponse { object IFactoryResponse.instance { get; set; } string IFactoryResponse.instanceconfig { get; set; } }
When interface member is implemented explicitly you have to look at variable as the interface, either by casting your class instance to that interface or assigning it into a variable types as that interface.
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; }
Why do you need the interface to be implemented explicitly? You shouldn't do that unless you have very good reason. With implicit implementation everything is much easier:
public class FactoryResponseImpl : IFactoryResponse { public object instance { get; set; } public string instanceconfig { get; set; } }
And your other code should work just fine.
Declaring Explicit interfaces and the does not contain a definition error C#
-
03-10-2022 - |
Question
I am having an issue with an explicit interface that I created and am getting the exception,
'x' does not contain a definition for 'y' and no extension method 'y' accepting a first argument of type 'x' could be found
I have a series of classes. The base class:
public interface IFactoryResponse
{
object instance { get; set; }
string instanceconfig { get; set; }
}
The class that explicitly implements it:
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; }
}
}
and in another class I get the above error. Visual studio can find the interface and class ok, but it can't resolve the instance property. What am I missing here. I am probably missing one of the more refined rules of explicit inheritance.
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;
}
Solution
OTHER TIPS
Your explicit implementations are referencing themselves. You should be referencing a private field or the public implementation. E.g.:
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;
}
}
}
Is this what you where aiming for?
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;
}
}
If one uses explicit interface implementations like IFactoryResponse.instance, then these methods are not publicly visible. Either you need to cast to IFactoryResponse to access them or define the methods as public: public object instance { ... }.