首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >隐式修饰接口所公开的方法以实现智能化

隐式修饰接口所公开的方法以实现智能化
EN

Stack Overflow用户
提问于 2014-04-12 04:21:14
回答 4查看 84关注 0票数 1

我有一个微不足道的问题,但我似乎想不出解决方案:

代码语言:javascript
复制
public interface ISimpleInterface
{
    String GetDayOfWeek();
    String GetLocalTime();
    // a few more hundreds of those
}

public class MyClass : ISimpleInterface, IVeryComplexInterface
{
   // implementations go here
}

MyClass mc = new MyClass();
var day = mc.GetDayOfWeek();

所以我想要做的是,当我在Visual Studio中编辑我的代码,并获得将出现成百上千个方法的mc.点时,能够(也许通过IntelliSense )区分每个方法是由ISimpleInterface定义的还是由IVeryComplexInterface定义的。

我知道我可以在mc.中对结果进行分类,并在额外的层(例如mc.SimpleMethods.GetDayOfWeek())上公开它们,但这不是我在这里试图实现的目标。

理想情况下,我希望用一个简短的字符串描述来装饰每个接口,然后在IntellliSense中显示出来,并且对这个接口的所有方法都是通用的(比如“这个方法属于ISimpleInterface")。

有什么明显的方法可以做到这一点吗?

EN

回答 4

Stack Overflow用户

发布于 2014-04-12 04:29:03

您可以向每个方法添加文档注释,这样您就可以获得有关每个调用的更好信息,并且可以将接口名称放入描述中

代码语言:javascript
复制
public interface ISimpleInterface
{
   /// <summary>As part of ISimpleInterface this method returns... </summary> 
   String GetDayOfWeek();

有时仅仅使用正确类型的变量可能就足够了:

代码语言:javascript
复制
 ISimpleInterface mc = new MyClass();
票数 3
EN

Stack Overflow用户

发布于 2014-04-12 04:28:42

我想要做到这一点的唯一方法是将你的对象转换到那个接口:

代码语言:javascript
复制
var simpleInterface = ((ISimpleInterface)mc);
// access your object

不过,可能还有其他我不知道的事情。

票数 2
EN

Stack Overflow用户

发布于 2014-04-12 04:32:37

您还可以使用文档来修饰您的方法:

代码语言:javascript
复制
<#@ template debug="true" hostspecific="true" language="C#" #>
<#@ output extension=".vsdocs.cs" #>

<#@ assembly name="EnvDTE" #>
<#@ assembly name="System.Core" #>

<#@ import namespace="System.IO" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="Microsoft.CSharp" #>
<#@ import namespace="System.CodeDom.Compiler" #>
<#@ import namespace="System.Reflection" #>
<#@ import namespace="System.CodeDom.Compiler" #>
<#@ import namespace="System.Reflection" #>

<#
//System.Diagnostics.Debugger.Break();

// Setup Environment
var dte = (EnvDTE.DTE)((IServiceProvider)Host).GetService(typeof(EnvDTE.DTE));
var project = dte.Solution.FindProjectItem(Host.TemplateFile).ContainingProject;

string path = this.Host.ResolvePath("");
string binpath = Path.Combine(path, "bin");

// Setup Loading File
string IBarCs = Path.Combine(path, "IBar.cs");
string IBarDll = Path.Combine(binpath, "IBar.Dll");
string IBarName = "ConsoleApplication10.IBar";

// Compile File to Temporary DLL
var cProvider = CodeDomProvider.CreateProvider("CSharp");

CompilerParameters cp = new CompilerParameters();
cp.GenerateExecutable = false;
cp.OutputAssembly = IBarDll;

CompilerResults cr = cProvider.CompileAssemblyFromFile(cp, IBarCs);

if(cr.Errors.Count > 0)
{
    // Display compilation errors.
    WriteLine("// Errors building {0} into {1}",  IBarCs, cr.PathToAssembly);
    foreach(CompilerError ce in cr.Errors)
    {
        WriteLine("//  {0}", ce.ToString());
    }
}

// Load File for Reflection
var ibar = Assembly.LoadFile(IBarDll);

var iBarType = ibar.GetType(IBarName);
    WriteLine("// " + iBarType.ToString());

var methods = iBarType.GetMethods().ToList();

foreach (var method in methods)
{
    WriteLine("// " + method);
}

#>

// Empty File
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23021903

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档