我有一个小问题,但我似乎无法提出解决方案:
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")。
有没有明显的方法可以做到这一点?
答案 0 :(得分:3)
您可以为每个方法添加文档注释,以便您可以获得有关每个调用的更好信息,并且可以将接口名称放入描述中
public interface ISimpleInterface
{
/// <summary>As part of ISimpleInterface this method returns... </summary>
String GetDayOfWeek();
有时只使用正确类型的变量就足够了:
ISimpleInterface mc = new MyClass();
答案 1 :(得分:2)
我认为这样做的唯一方法是将对象转换为该接口:
var simpleInterface = ((ISimpleInterface)mc);
// access your object
虽然可能还有其他我不知道的东西......
答案 2 :(得分:1)
您还可以使用文档装饰您的方法:
<#@ 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
答案 3 :(得分:1)
怎么样:
MyClass mc = new MyClass();
var day = mc.SimpleMethods.GetDayOfWeek();
var anyOtherThing = mc.SimpleMethods.GetAnyOtherThing();
达成:
public class MySimpleClass : ISimpleInterface
{
// implementations go here
}
public class MyVeryComplexClass : ISimpleInterface
{
// implementations go here
}
public class MyClass
{
public MySimpleClass SimpleMethods = new MySimpleClass();
public MyVeryComplexClass ComplexMethods = new MyVeryComplexClass();
}
这种方式MyClass
是一种aggregate,包含更小,更专业的类。通过这样做,您也可以获得更高的Single Responsability Principle。
虽然名称SimpleMethods
和ComplexMethods
可能会更好。