使用C#接口 [英] Using C# Interfaces

查看:69
本文介绍了使用C#接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读有关WinForms第三方控件的一些文档。我正在看的是一个搜索控件,基本上它只是一个文本框,你可以输入一些数据,它将搜索另一个控件,前提是控件正在实现接口的
。所以,假设我有一个网格控件,甚至是一个实现 的listView控件。从 界面然后我现在可以搜索他们的数据。  

I was reading some documentation on third-party controls on WinForms. The one I was looking at was a search control, basically it is just a textbox where you can type in some data and it will search against another control provided that the control is implementing the interface. So, let's say I have a grid control or even a listView control that implements  from  interface then I can now search their data.  

{ List<string> Data { get;} } public class FirstData : IData { public List<string> Data { get { List<string> data=new List<string>(); data.Add("a"); data.Add("b"); data.Add("c"); return data; } } } public class SecondData : IData { public List<string> Data { get { List<string> data = new List<string>(); data.Add("d"); data.Add("e"); data.Add("f"); return data; } } }

//获取数据

public List< string> GetData(IData数据)
{
return data.Data;
}

public List<string> GetData(IData data)
{
return data.Data;
}

虽然,这不使用真正的控件,但我想你明白了。编码会工作正常,但我正在寻找一个更好的方法如何做到这一点。其他开发人员可能会检查一个类是否实现了所述接口,但我真的不知道那个
以及它与我上面的代码有什么不同。 

Though, this doesn't use a real control but i guess you get the idea.Code will work fine though but i am looking for a better way how to do this right. Other developers might check if a class implements the said interface but i really don't know that yet and how does it differ from my code above. 

我也做还有一个问题是使用接口,因为我只知道当接口有一个属性或方法时如何使用接口。 
$


假设我有一个像这样的示例代码

I also do have another concern using interfaces because i only know how to use interface when that interface does have a properties or methods. 

Let's say i have a sample code like this

public interface ISetting
{

}

public class ApplicationSetting:ISetting
{
    string Setting1 {get;set;}
    string Setting2 {get;set;}
}


public class AnotherSetting : ISetting
{ 
    string AnotherSetting1{get;set;}
    string AnotherSetting2{get;set;}
}

我是否使用它,我只知道该类实现了接口,但我真的不知道实现该接口的类的属性是什么。

How do i use this, i only know that the class implements the interface but i really don't have an idea what are the properties of the class that implements the interface.

问候










推荐答案

Hi Dikong42,

Hi Dikong42,

问题的第一部分:

"虽然  这不使用真正的控制,但是  猜你得到了想法
c
ode  可以正常工作,但我 正在寻找更好的方法如何做到这一点。"

接口已经实现,因为它应该是唯一应该改变的是您的属性getter: 

the interface is already implemented as it should the only thing that you should change is your properties getters : 

public class FirstData: IData
{
        // always initialize backing fields in the constructor
        // 
        public FirstData()
        {
                data = new List<string>();
		data.Add("a");
		data.Add("b");
		data.Add("c");
        }
        // consider using a backing field if you need to 
        // implement a kind of validation or any interaction while setting values 
        // otherwise auto-properties will generate the same IL with a backing field
	List<string> data;
	public List<string> Data
	{
		get
		{
			return data;
		}	
	}
}

第二部分关于没有成员的接口,这些用于"让我们说"类的标志,它使我们能够通过反射搜索它们:

for the second part about interfaces without members, those are used to "let say a flag for the class", which enables us to search them through reflection:

var type = typeof(IEntity);
var types = AppDomain.CurrentDomain.GetAssemblies()
    .SelectMany(s => s.GetTypes())
    .Where(p => type.IsAssignableFrom(p));

这是一篇解释情况的文章:  让C#类实现接口

here is an article explaining the situation: get C# classes implementing interface,

最好的问候,

Mouad。

良好的编码;


这篇关于使用C#接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆