C# 创建接口的实例

C# 创建接口的实例,c#,interface,C#,Interface,我定义了以下接口: public interface IAudit { DateTime DateCreated { get; set; } } public interface IAuditable { IAudit Audit { get; set; } } IAuditable界面说明我将对哪些类进行审核。IAudit接口是该类的实际Audit。例如,假设我有以下实现: public class User : IAuditable { public string

我定义了以下接口:

public interface IAudit {
    DateTime DateCreated { get; set; }
}

public interface IAuditable {
    IAudit Audit { get; set; }
}
IAuditable
界面说明我将对哪些类进行
审核。
IAudit
接口是该类的实际
Audit
。例如,假设我有以下实现:

public class User : IAuditable {
    public string UserName { get; set; }
    public UserAudit Audit { get; set; }
}

public class UserAudit : IAudit {
    public string UserName { get; set; }
    public DateTime DateCreated { get; set; }

    public UserAdit(User user) {
        UserName = user.UserName;
    }
}
现在给定一个
IAuditable
(上面的
User
)的对象,我希望能够通过将
IAuditable
对象输入构造函数来创建一个
IAudit
UserAdit
)。理想情况下,我会有类似于:

if (myObject is IAuditable) {
    var audit = new IAudit(myObject) { DateCreated = DateTime.UtcNow }; // This would create a UserAudit using the above example
}
但是我有一大堆问题:

  • 无法创建接口的实例
  • 代码中没有定义哪个
    IAudit
    应用于哪个
    IAuditable
  • 我不能指定
    IAudit
    接口必须有一个接受
    IAuditable
我确信这是一种很多人都有过的设计模式,但我无法理解它。如果有人能告诉我如何做到这一点,我将不胜感激

无法创建接口的实例

对。创建实现接口的对象的实例:

IAuditable myUser = new User();

代码中没有定义哪个IAudit应用于哪个IAuditable

您不能仅使用一个接口直接执行此操作。你需要重新考虑你的设计

您可以在接口中使用打开的泛型类型,并使用关闭的类型实现它:

public interface IAudit<T> {
    DateTime DateCreated { get; set; }
}

public class UserAudit : IAudit<User> {
    public string UserName { get; set; }
    public DateTime DateCreated { get; set; }

    public UserAdit(User user) {
        UserName = user.UserName;
    }
}
公共接口IAudit{
DateTime DateCreated{get;set;}
}
公共类UserAudit:IAudit{
公共字符串用户名{get;set;}
public DateTime DateCreated{get;set;}
公共用户ADIT(用户){
用户名=user.UserName;
}
}

我不能指定IAudit接口必须具有接受IAuditable的构造函数


没错,你不能。看见您需要在实现者上创建这样的构造函数。

一种方法是使用泛型

一旦您有了一个实现I接口并具有公共无参数构造函数的T类型,您就可以创建T的实例:

public void Create<T> where T : IAudit, new()
{
     T instance = new T();
     // TODO: Your logic using such T instance of IAudit implementation
} 
public void创建,其中T:IAudit,new()
{
T实例=新的T();
//TODO:使用IAudit实现的此类T实例的逻辑
} 
我不知道您的代码体系结构或目标,但您可以创建一个类似上面的工厂方法来创建IAudit对象的实例

代码中没有定义哪个IAudit应用于哪个IAudit 可审计 我无法指定IAudit接口必须具有 接受IAuditable的构造函数

您可以通过在
IAuditable
中添加
CreateAudit()
函数来修复这两个问题。然后您将获得一个从
IAuditable
创建的
IAudit
。如果您想在
IAuditable
中存储对
IAudit
的引用(或者反之亦然),这样您就可以让它们相互关联,那么让一个实现类这样做非常容易。例如,您还可以将
GetAuditable()
添加到
IAudit
以获取创建它的
IAuditable

简单实现如下所示(在实现IAuditable的类上):


显然,您无法创建接口的实例,但如果您确实试图创建传入类的实例,则可以执行以下操作:

IAuditable j = ((IAuditable)Activator.CreateInstance(myObject.GetType()));
您需要知道要构造哪个具体类,在您的示例中,唯一的选项是myObject


或者,您可以研究一种叫做“依赖注入”的东西,它允许您指定将哪种类型的具体类“注入”到调用构造函数或字段中接口的参数中。我不确定你的总体设计,所以这可能是适用的。在依赖性注入中,您可以在代码中声明IAuditable应该使用UserAudit创建,尽管这比简单地调用“new IAuditable”稍微多一些连线。如果您需要为接口创建实例,您可以创建一个名为FactoryClass()的公共类,其中包含所有接口方法。您可以使用以下代码:

public class FactoryClass  //FactoryClass with Interface named "IINterface2"
{
   public IInterface2 sample_display()   //sample_display() is a instance method for Interface
   {
       IInterface2 inter_object = null;   //assigning NULL for the interface object
       inter_object = new Class1();       //initialising the interface object to the class1()
       return inter_object;   //returning the interface object
   }
}
在Main()类中添加以下代码:

IInterface2 newinter; //creating object for interface in class main()
FactoryClass factoryobj=new FactoryClass();  // creating object for factoryclass
newinter.display() //display() is the method in the interface.
在构造函数中添加以下内容:

newinter=factoryobj.sample_display() // initialisingg the interface object with the instance method of factoryobj of FACTORY CLASS.
您可以使用以下代码调用fffunction:

IInterface2 newinter; //creating object for interface in class main()
FactoryClass factoryobj=new FactoryClass();  // creating object for factoryclass
newinter.display() //display() is the method in the interface.

您可以在范围之外创建一个静态接口变量,然后在代码中使用它

static IRequestValidator ir;


static void Main(string[] args)...

+1正是我要发布的答案,但你似乎更快地理解了这个问题。@nfplee:你问的更多的是一个设计问题,而你想要的答案并不在于c#/.net的细节。这是你的答案,或者至少让你找到了正确的方向。@Tridus:谢谢你的答案,但现在已经是周五下午的晚些时候了,显然我的大脑已经崩溃了。如果我试图说((IAuditable)myObject.CreateAudit()。找不到CreateAudit方法。我相信我已经完成了你所说的一切。@Tridus:请不要理我,我真的应该学会更多地构建,更少地依赖intellisense lol。再次感谢你的帮助。有用的答案,对OP有用的信息,但仍然过多地关注c#/.net的细节以获得我的支持。。。现在。Tridus'解决了IMO的真正问题。不过,对于OP来说,还是有用的信息。嗨,干杯,我已经接受了@Tridus的回答,但我已经使用了你建议的部分内容来帮助你。