我已经多次看到从类生成的Interface实例.为什么以这种方式使用接口?Interface实例仅在派生类的帮助下创建,我们只能通过此实例访问此接口成员.这有什么优势?我很困惑..
interface IPrint
{
void Print();
}
class Sample : IPrint
{
public void Print()
{
Console.WriteLine("Print...");
}
public void Sample()
{
Console.WriteLine("Sample...");
}
}
class Program
{
static void Main(string[] args)
{
IPrint print = new Sample();
print.Print();
}
}
Run Code Online (Sandbox Code Playgroud) 我在其他文件夹中调用PartialView而不是Views文件夹时,我得到"必须从WebViewPage派生"错误.
错误
System.InvalidOperationException: The view at '~/Modules/HtmlContent/_HtmlContent.cshtml' must derive from WebViewPage, or WebViewPage<TModel>.
PartialView adding
Run Code Online (Sandbox Code Playgroud)
Page.cshtml
<div class="side">
@Html.Action("Side")
</div>
Run Code Online (Sandbox Code Playgroud)
HomeController.cs
public ActionResult Side()
{
return PartialView("~/Modules/HtmlContent/_HtmlContent.cshtml");
}
Run Code Online (Sandbox Code Playgroud)
文件层次结构
Modules
|
+-- HtmlContent
| |
| +-- _HtmlContent.cshtml
|
Views
|
+-- Home
| |
| +-- Index.cshtml
| |
| +-- Page.cshtml
|
+-- Shared
|
+-- Layout.cshtml
|
+-- _Partial.cshtml
Run Code Online (Sandbox Code Playgroud)