我如何错误检查该工厂 Create使用带有参数的激活器的方法?
我不确定在这段代码中调用激活器之前是否/如何检查构造函数是否确实存在(未经测试,因此可能有错误,但希望意图很明确)。
就好像我想要一个模板约束,表示“其中 T 有一个带有签名 S 的构造函数”。
public class EntityContainerFactory
{
public EntityContainerFactory(string sqlServerName, string databaseName, string metaData)
: this(sqlServerName, databaseName, metaData, "System.Data.EntityClient")
{
}
public EntityContainerFactory(string sqlServerName, string databaseName, string metaData, string dataProviderName)
{
SqlServerName = sqlServerName;
DatabaseName = databaseName;
Metadata = metaData;
DataProviderName = dataProviderName;
}
// --> IS THERE ANY WAY TO CHECK THAT T HAS
// A CONSTRUCTOR THAT TAKES AN ENTITY CONNECTION?
public T Create<T>()
{
return (T)Activator.CreateInstance(typeof(T), CreateEntityConnection());
}
EntityConnection CreateEntityConnection()
{
SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder();
sqlBuilder.DataSource = SqlServerName;
sqlBuilder.InitialCatalog = DatabaseName;
sqlBuilder.IntegratedSecurity = true;
EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
entityBuilder.Provider = DataProviderName;
entityBuilder.ProviderConnectionString = sqlBuilder.ToString();
entityBuilder.Metadata = Metadata;
return new EntityConnection(entityBuilder.ConnectionString);
}
public string DatabaseName { get; set; }
public string SqlServerName { get; set; }
public string DataProviderName { get; set; }
private string metaData;
public string Metadata
{
get
{
string result;
if (!this.metaData.StartsWith("res://"))
{
result = string.Format(@"res://*/{0}.csdl|res://*/{0}.ssdl|res://*/{0}.msl", this.metaData);
}
else
{
result = this.metaData;
}
return result;
}
set
{
this.metaData = value;
}
}
}
I'm not sure if/how I can check that the constructor actually exists before calling activator in this code (untested so might have bugs but hopefully the intention is clear).
Its as if I wanted a template constraint that says "where T has a constructor with signature S".
public class EntityContainerFactory
{
public EntityContainerFactory(string sqlServerName, string databaseName, string metaData)
: this(sqlServerName, databaseName, metaData, "System.Data.EntityClient")
{
}
public EntityContainerFactory(string sqlServerName, string databaseName, string metaData, string dataProviderName)
{
SqlServerName = sqlServerName;
DatabaseName = databaseName;
Metadata = metaData;
DataProviderName = dataProviderName;
}
// --> IS THERE ANY WAY TO CHECK THAT T HAS
// A CONSTRUCTOR THAT TAKES AN ENTITY CONNECTION?
public T Create<T>()
{
return (T)Activator.CreateInstance(typeof(T), CreateEntityConnection());
}
EntityConnection CreateEntityConnection()
{
SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder();
sqlBuilder.DataSource = SqlServerName;
sqlBuilder.InitialCatalog = DatabaseName;
sqlBuilder.IntegratedSecurity = true;
EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
entityBuilder.Provider = DataProviderName;
entityBuilder.ProviderConnectionString = sqlBuilder.ToString();
entityBuilder.Metadata = Metadata;
return new EntityConnection(entityBuilder.ConnectionString);
}
public string DatabaseName { get; set; }
public string SqlServerName { get; set; }
public string DataProviderName { get; set; }
private string metaData;
public string Metadata
{
get
{
string result;
if (!this.metaData.StartsWith("res://"))
{
result = string.Format(@"res://*/{0}.csdl|res://*/{0}.ssdl|res://*/{0}.msl", this.metaData);
}
else
{
result = this.metaData;
}
return result;
}
set
{
this.metaData = value;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否考虑过使用接口而不是工厂?您可以获得更好的结果,并且不会遇到这样的问题。
无论如何:
仅适用于无参数构造函数。当您遇到这种限制时,您可以不加思考地执行以下操作:
您将必须手动检查。
Have you considered using interfaces instead of factories? You can get much better results, and you don't run into problems like this.
Anyway:
Only works for parameterless constructors. When you have that constraint, you can do the following, without reflection:
You're going to have to check manually.
不,你不能 - 唯一可用的与构造函数相关的通用约束是 new() ,它需要无参数构造函数。
一种选择是需要工厂委托:
然后您可以使用:
或任何其他将构建适当对象的东西。它更灵活、更安全、性能也更好。 (诚然,后者可能无关紧要......)
No, you can't - the only constructor-related generic constraint available is
new()
which requires a parameterless constructor.One option is to require a factory delegate instead:
Then you can use:
or anything else that will build the appropriate object. It's more flexible, safer, and performs better too. (The latter is probably irrelevant, admittedly...)
Type.GetConstructor 方法 (Type())
Type.GetConstructor Method (Type())