简体   繁体   English

从dll访问用户控件类型?

[英]Access user control type from a dll?

I have a user control (under a website project) 我有一个用户控件(在一个网站项目下)

UcArticle.ascx UcArticle.ascx

<%@ Control Language="C#" 
            AutoEventWireup="true"
            CodeFile="UcArticle.ascx.cs" 
            Inherits="Uc_UcArticle" 
            ViewStateMode="Disabled" %>
...

And the code Behind is : 而后面的代码是:

UcArticle.ascx.cs UcArticle.ascx.cs

public partial class Uc_UcArticle : System.Web.UI.UserControl
{
   ...
}

However , I have a DLL project and a method inside it which should use the type UcArticle : 但是,我有一个DLL项目和一个其中应使用UcArticle类型的UcArticle

  public void AddMethod(UcArticle uc ) //error here
        {
         //do something with uc...

        }

But the dll doesn't know UcArticle ( Cannot resolve symbol) 但是dll不知道UcArticle (无法解析符号)

ps I could use dynamic to access the properties of uc , but I don't want to do that. ps我可以使用dynamic来访问uc的属性,但是我不想这样做。

How can I make the dll know the type UcArticle ? 如何使dll知道UcArticle类型?

When you have a project that contains a UC and the project depends on an assembly that needs the UC, then you have a circular dependency. 当您有一个包含UC的项目并且该项目依赖于需要UC的程序集时,您将具有循环依赖关系。

The best way to break this is to put the UC in yet another, separate, assembly. 打破此问题的最佳方法是将UC放入另一个单独的组件中。

But it remains a smell that any part of your business logic should need to know about a piece of the UI. 但是,仍然有一种气味,您的业务逻辑的任何部分都需要了解一部分用户界面。 So do rethink the design that leads you here. 因此,请重新考虑将您引向此处的设计。 Something's rotten. 东西烂了。

Well, i think this is a bad design. 好吧,我认为这是一个不好的设计。

In you AddMethod function, do you really need your customized user control type? 在您的AddMethod函数中,您是否真的需要自定义的用户控件类型? why not declare an interface, make your customized user control implements the interface, also change the AddMethod function to carry the interface type as parameter. 为什么不声明一个接口,让您的自定义用户控件实现该接口,还更改AddMethod函数以将接口类型作为参数。 just expose whatever operation you want to outside. 只需将您想要执行的任何操作暴露给外部即可。

what do you think? 你怎么看?

updated(i just type directly here, so, sorry for the format): 更新(我只是在这里直接键入,因此,对不起格式):

//well then you can declare the interface as:
public interface ITextControl{
 string TextValue{get;set}
}
//and in your code:
public partical class whateveryourcontrolname:UserControl,ITextControl
{
/..../
public string TextValue{
 get{
  return this.Text;
 }
set{
 this.Text=value;
 }
}
}
//and your method:
void AddMethod(ITextControl txtCtrl){
 txtCtrl.TextValue="Yes";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM