我有一个微不足道的问题,但我似乎想不出解决方案:
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")。
有什么明显的方法可以做到这一点吗?
发布于 2014-04-12 04:29:03
您可以向每个方法添加文档注释,这样您就可以获得有关每个调用的更好信息,并且可以将接口名称放入描述中
public interface ISimpleInterface
{
/// <summary>As part of ISimpleInterface this method returns... </summary>
String GetDayOfWeek();有时仅仅使用正确类型的变量可能就足够了:
ISimpleInterface mc = new MyClass();发布于 2014-04-12 04:28:42
我想要做到这一点的唯一方法是将你的对象转换到那个接口:
var simpleInterface = ((ISimpleInterface)mc);
// access your object

不过,可能还有其他我不知道的事情。
发布于 2014-04-12 04:32:37
您还可以使用文档来修饰您的方法:

<#@ 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 Filehttps://stackoverflow.com/questions/23021903
复制相似问题