c#,unity DI:接口属性不能访问扩展接口的类内部函数
本文关键字:接口 访问 扩展 内部函数 不能 属性 unity DI | 更新日期: 2023-09-27 17:52:16
我的接口只有一个函数和一个属性。类扩展了接口,然后我们可以从同一类中声明的函数访问该属性。这里我使用unity动态调用返回IDBConnection对象的函数。
当我编译代码,然后得到错误,如
名称"ConType"在当前上下文中不存在
我想不出我在哪里出错了。所以请有人看到我的完整代码,并告诉我我在哪里犯了错误,在哪里以及如何修复它。
类代码public interface BBAConnections
{
IDbConnection CreateConnection();
string ConType { get; set; }
}
public class BBAConnection : BBAConnections
{
string ConType { get; set; }
public IDbConnection CreateConnection()
{
string _connectionString = "";
IDbConnection connection=null;
if (ConType == "local")
{
_connectionString = "put here local db connection";
connection = new System.Data.SqlClient.SqlConnection(_connectionString);
}
else if (ConType == "remote")
{
_connectionString = "put here remote db connection";
connection = new System.Data.SqlClient.SqlConnection(_connectionString);
}
else if (ConType == "OrcsWeb")
{
_connectionString = "put here website db connection";
connection = new System.Data.SqlClient.SqlConnection(_connectionString);
}
else if (ConType == "Sage")
{
_connectionString = "put here sage connection";
connection = new System.Data.SqlClient.SqlConnection(_connectionString);
}
return connection;
}
}
public static class Factory
{
static IUnityContainer cont = null;
public static BBAConnections initialize(string type)
{
BBAConnections oDbConnection = null;
cont = new UnityContainer();
cont.RegisterType<BBAConnections, BBAConnection>("local");
cont.RegisterType<BBAConnections, BBAConnection>("remote");
cont.RegisterType<BBAConnections, BBAConnection>("OrcsWeb");
cont.RegisterType<BBAConnections, BBAConnection>("Sage");
oDbConnection = cont.Resolve<BBAConnections>(type);
oDbConnection.ConType = type;
return oDbConnection;
}
}
像这样调用CreateConnection函数
Factory.initialize("local").CreateConnection();
首先, c#是大小写敏感的,所以您应该将所有IDbConnection更改为IDBConnection。
现在,你的接口说任何从它继承的东西必须有一个CreateConnection方法和一个ConType属性。在你的bbacontion类中,你应该创建一个名为ConType的公共字符串属性。
应该修复你的代码,它应该是这样的:
public interface IDBConnection
{
IDBConnection CreateConnection();
string ConType { get; set; }
}
public class BBAConnection : IDBConnection
{
public string ConType { get; set; }
public IDBConnection CreateConnection()
{
string _connectionString = "";
IDBConnection connection = null;
if (ConType == "local")
{
_connectionString = "put here local db connection";
connection = new System.Data.SqlClient.SqlConnection(_connectionString);
}
else if (ConType == "remote")
{
_connectionString = "put here remote db connection";
connection = new System.Data.SqlClient.SqlConnection(_connectionString);
}
else if (ConType == "OrcsWeb")
{
_connectionString = "put here website db connection";
connection = new System.Data.SqlClient.SqlConnection(_connectionString);
}
else if (ConType == "Sage")
{
_connectionString = "put here sage connection";
connection = new System.Data.SqlClient.SqlConnection(_connectionString);
}
return connection;
}
}
public static class Factory
{
static IUnityContainer cont = null;
public static IDBConnection initialize(string type)
{
IDBConnection oDbConnection = null;
cont = new UnityContainer();
cont.RegisterType<IDBConnection, BBAConnection>("local");
cont.RegisterType<IDBConnection, BBAConnection>("remote");
cont.RegisterType<IDBConnection, BBAConnection>("OrcsWeb");
cont.RegisterType<IDBConnection, BBAConnection>("Sage");
oDbConnection = cont.Resolve<IDBConnection>(type);
oDbConnection.ConType = type;
return oDbConnection;
}
}
将实现类中的声明更改为
public string ConType { get; set; }