小编skr*_*was的帖子

C#中的接口继承

我正在尝试在编写应用程序时遇到的问题超出了(对我来说)很大的问题.请看这个(我会尽量缩短代码):

我有root接口叫IRepository<T>.接下来,IBookRepository : IRepository<Book> Next,实现它的具体类:BookRepository : IBookRepository

在我声明的RepositoryManager类中 private IRepository<IRepoItem> currentRepo;

IRepoItem 是一个由Book类实现的接口.

现在,当我尝试做这样的事情时:

currentRepo = new BookRepository();
Run Code Online (Sandbox Code Playgroud)

VisualStudio提供错误消息:

无法将类型'BookRepository'隐式转换为'IRepository'.存在显式转换(您是否错过了演员?)

我尝试显式转换,但抛出运行时异常...

我知道(几乎可以肯定)它是一种叫做协方差的东西,这可能(可能)在.Net 4.0中解决了.不幸的是我正在使用框架版本3.5编写,我无法改变它.请给我一些建议怎么做 - 如何超支这个问题?我想从RepoFactory获得currentRepo,它会产生几种存储库,具体取决于用户的需求.我不知道这里是否允许链接,但我在http://olgatherer.wordpress.com/上写了一些博客,我在其中描述了应用程序的创建.我的英语不好,但我希望能够理解我.提前谢谢你的回答.

最好的问候,skrzeczowas

c# oop interface covariance

10
推荐指数
1
解决办法
756
查看次数

如何指向通用抽象类?

自从我上一次使用接口协方差问题以来,我没有遇到代码问题.今天我采用了另一种OLGAtherer架构的方法,我发现了巨大的(对我来说)障碍.我希望它只对我很大,我会在这里找到答案:)好的,请检查一下:

我有抽象的泛型类:

abstract class Repository<T> where T : Entity
Run Code Online (Sandbox Code Playgroud)

此类包含四个CRUD方法(添加,插入等)和两个受保护字段的签名.我将创建的任何存储库都必须从此超类继承.我有一些存储库,例如:

class BookRepository : Repository<Book>
Run Code Online (Sandbox Code Playgroud)

现在我有类DB负责DB文件的操作.而且有我的问题.看看放在DB类中的这个方法:

    void Add(List<Entity> enitites, Type entityType)
    {
       Repository<entityType> repo = EntitiesFactory(entityType);
       repo.Add(entities);
    }
Run Code Online (Sandbox Code Playgroud)

我知道上面的方法不起作用,但这可视化我的问题(希望) - 我想动态创建指定类型的存储库,但我不知道如何做到这一点.这一直是我的主要OOP问题 - 我可以编写继承(或接口的实现),但我不能使用这些继承的类.请向我澄清一下.先感谢您.

帕维尔


我使用了你提供的提示,这就是我现在所拥有的:

public void Add<T>(List<T> entities, string repoName) where T : Entity
{
   Repository<T> repo = RepoFactory<T>(repoName, typeof(T));
   repo.Add(entities);
}

private Repository<T> RepoFactory<T>(string repoName, Type t) where T : Entity
{
   if (t == typeof(Book))
   {
   Repository<T> repo = (Repository<T>)((object)(new BookRepository(this.ConnectionString, repoName)));
   }

   return null;
Run Code Online (Sandbox Code Playgroud)

}

RepoFactory现在在DB类中(顺便说一句,这里是某种类型的Bridge) - …

c# oop generics inheritance class

1
推荐指数
1
解决办法
306
查看次数

如何提供WebBrowser控件和操作HTML文档?

美好的一天

我有关于在Windows窗体应用程序中显示html文档的问题.我正在处理的应用程序应显示来自的信息

html格式的数据库.我将尝试描述我已经采取的行动(以及哪些失败):

1)我试图加载仅存在于内存中的"虚拟"html页面并动态更改它的参数(webbMain是一个WebBrowser控件):

public static string CreateBookHtml()
{
 StringBuilder sb = new StringBuilder();

 //Declaration
 sb.AppendLine(@"<?xml version=""1.0"" encoding=""utf-8""?>");
 sb.AppendLine(@"<?xml-stylesheet type=""text/css"" href=""style.css""?>");
 sb.AppendLine(@"<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.1//EN"" 
                    ""http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"">");
 sb.AppendLine(@"<html xmlns=""http://www.w3.org/1999/xhtml"" xml:lang=""en"">"); 

 //Head
 sb.AppendLine(@"<head>");
 sb.AppendLine(@"<title>Exemplary document</title>");
 sb.AppendLine(@"<meta http-equiv=""Content-Type"" content=""application/xhtml+xml; 
                                                                charset=utf-8""/   >");
 sb.AppendLine(@"</head>");

 //Body
 sb.AppendLine(@"<body>");
 sb.AppendLine(@"<p id=""paragraph"">Example.</p>");
 sb.AppendLine(@"</body>");
 sb.AppendLine(@"</html>");

 return sb.ToString();
}
Run Code Online (Sandbox Code Playgroud)

void LoadBrowser(){this.webbMain.Navigate("about:blank"); this.webbMain.DocumentText = CreateBookHtml(); HtmlDocument doc = this.webbMain.Document; }

这失败了,因为doc.Body为null,而doc.getElementById("paragraph")也返回null.所以我无法更改段内的InnerText属性.

此外,this.webbMain.DocumentText为"\ 0"......

2)我尝试在指定的文件夹中创建html文件,将其加载到WebBrowser,然后更改其参数.Html与创建的相同

CreateBookHtml()方法:

private void LoadBrowser()
        {
            this.webbMain.Navigate("HTML\\BookPage.html"));             
            HtmlDocument doc = this.webbMain.Document;
        }
Run Code Online (Sandbox Code Playgroud)

这次this.webbMain.DocumentText包含从文件中读取的Html数据,但doc.Body再次返回null,我仍然无法使用元素

getByElementId()方法.当然,当我有文本时,我会尝试正则表达式获取指定的字段,或者可能做其他技巧来实现目标,但我想知道 - 有没有简单的方法来主化html?对我来说,理想的方法是在内存中创建HTML文本,将其加载到WebBrowser控件中,然后使用ID动态更改其参数.可能吗?感谢您的回答,最好的问候,

帕维尔

html c# browser dom

1
推荐指数
1
解决办法
1万
查看次数

标签 统计

c# ×3

oop ×2

browser ×1

class ×1

covariance ×1

dom ×1

generics ×1

html ×1

inheritance ×1

interface ×1