任何人都可以说如何使用一个项目的Dll作为另一个项目的代码 [英] Can anyone say how to use Dll of one project as a code behind of another project

查看:61
本文介绍了任何人都可以说如何使用一个项目的Dll作为另一个项目的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目只有一个班级。并使用一个带有按钮的Web表单创建另一个项目。我想把第一个项目的类作为第二个项目的代码。我怎样才能做到这一点?我不想在我的项目中添加我的DLL作为参考。

解决方案

按照步骤,





  1. 创建的C#(cs)文件应该是部分文件并从System.Web.UI.Page类继承,如下所示

    < pre lang =c#> public partial class 测试:System.Web.UI.Page
    {
    }

  2. 然后是前端(.aspx)文件,应该引用你的类文件< page>的CodeBehind属性标记
  3. 此外,Inherits属性应通过您的名称空间引用您的类,如下所示

     <%@  < span class =code-attribute>   Page    语言  =  C#    AutoEventWireup   =  true    CodeBehind   =  Testing.aspx.cs   继承  =  WebApplication1.Testing   %>  


您不能因为System.Web.UI.Page的方法受到保护。你只能继承来使用这些。



如果第一个项目类(我们将调用class1)只是继承System.Web.UI.Page然后没有标准方法将可用于第二个项目(class2),即使你将它作为参考添加。



您可以为class1创建一个定制的类类型,以便您可以公开您要共享的方法。这可以通过继承和使用反射加载class1来完成。



为此,class1和class2都需要一个共享的引用点,以便他们知道class1是什么样的。您可以创建一个简单的界面来执行此操作:



 界面 IPage 
{
void LoadMethod();
void InitMethod();
bool MyMethod();
}





Class1可以实现此接口并将其用于自己的结构:



  public   partial   class  Class1:System.Web.UI.Page,IPage 
{
protected void Page_Load( object sender,EventArgs e)
{
LoadMethod();
}

受保护 覆盖 void OnInit(EventArgs e)
{
base .OnInit(e);
InitMethod();
}

public void LoadMethod()
{
// 共享内容
}

public void InitMethod()
{
// 共享内容
}

public bool MyMethod()
{
// 共享内容
return true ;
}
}







然后你可以使用反射在Class2中加载Class1并以相同的方式使用相同的方法(这些更复杂,所以我把它分解成几种方法:



  public   partial   class  Class2:System.Web.UI。 Page 
{
private IPage _page = GetPage( mydll myclass);

受保护 void Page_Load( object sender,EventArgs e)
{
_page.LoadMethod();
}

protected 覆盖 void OnInit(EventArgs e)
{
base .OnInit(e);
_page.InitMethod();
}

受保护 虚拟 bool MyMethod()
{
return _page.MyMethod();
}

private 静态 IPage GetPage( string assemblyName, string className)
{
Assembly parser = GetAssembly(assemblyName);

类型type = parser.GetType(className);
if (type.IsAssignableFrom( typeof (IPage)))
{
IPage instance =(IPage)GetInstance(type);
返回实例;
}
throw new InvalidCastException( Api未实现类型 + typeof (IPage).Name) ;
}
私有 静态程序集GetAssembly( string trackerAssembly)
{
if (!string.IsNullOrEmpty(trackerAssembly))
{
string filename = trackerAssembly;
string extension = Path.GetExtension(filename);
if (!string.IsNullOrEmpty(extension)&&!extension.Equals( 。dll))
filename + = .DLL;
if (!File.Exists(filename))
{
string location = Path.GetDirectoryName(Assembly.GetExecutingAssembly()。Location);
if (!string.IsNullOrEmpty(location))
filename = Path.Combine(location, Apis,filename);
}
if (!File.Exists(filename))
throw new FileNotFoundException( 找不到文件 ,filename); // 引发错误?或者至少记录
return Assembly.LoadFrom(filename);
}
return Assembly.GetExecutingAssembly();
}
私有 静态 对象 GetInstance(类型页面)
{
if (!page.IsAssignableFrom( typeof (IPage)))
throw new ArgumentException( string .Format( 类型{0}未实现接口{1 }。,page.Name, typeof (IPage).Name), < span class =code-string> page
);

类型[] types = Type.EmptyTypes;

var constructorInfo = page.GetConstructor(types);
if (constructorInfo!= null
return constructorInfo.Invoke( null );
throw new ArgumentException( string .Format( 类型{0}没有与ctor参数匹配的ctor。 ,page.Name), page);
}
}







两个项目都需要参考IPage界面。您可以通过创建带有界面的第三个项目并在其他项目中引用它来实现此目的。



注意:他们必须使用相同的项目IPage。他们不应该有单独看起来相同的IPage接口。



希望有帮助


I have a project having only one class. And having another project with one web form with a button. I want to make the class of first project as the code behind of second project. How can I do that? I don't want to add my DLL as a reference in that project.

解决方案

Follow the steps,


  1. The created C#(cs) file should be partial and inherited from the System.Web.UI.Page class as below

    public partial class Testing : System.Web.UI.Page
    {
    }

  2. Then Front end(.aspx) file, should refer your class file by CodeBehind attribute of <page> tag
  3. Also Inherits attribute should refers your class by means of you namespaces, as below

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Testing.aspx.cs" Inherits="WebApplication1.Testing" %>


You cannot as the methods of System.Web.UI.Page are protected. You can only inherit to make use of these.

If the first project class (we'll call class1) just inherits System.Web.UI.Page then none of the standard methods will be available to the second project (class2) even if you add it as a reference.

You can create a bespoke class type for class1 so that you can expose methods you want to share. This can be done with inheritance and loading the class1 using reflection.

To do this, both class1 and class2 will need a shared point-of-reference so that they know what class1 looks like. You can create a simple Interface to do this:

interface IPage
{
    void LoadMethod();
    void InitMethod();
    bool MyMethod();
}



Class1 can the implement this interface and use it for its own structure:

   public partial class Class1 : System.Web.UI.Page , IPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadMethod();
        }

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        InitMethod();
    }

    public void LoadMethod()
    {
        //shared stuff
    }

    public void InitMethod()
    {
        //shared stuff
    }

    public bool MyMethod()
    {
        //shared stuff
        return true;
    }
}




You can then load Class1 in Class2 using reflection and use the same methods in the same way (this ones more complex so I broke it down into several methods :

   public partial class Class2 : System.Web.UI.Page
    {
        private IPage _page = GetPage("mydll", "myclass");

    protected void Page_Load(object sender, EventArgs e)
    {
        _page.LoadMethod();
    }

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        _page.InitMethod();
    }

    protected virtual bool MyMethod()
    {
        return _page.MyMethod();
    }

    private static IPage GetPage(string assemblyName, string className)
    {
        Assembly parser = GetAssembly(assemblyName);

        Type type = parser.GetType(className);
        if (type.IsAssignableFrom(typeof(IPage)))
        {
            IPage instance = (IPage)GetInstance(type);
            return instance;
        }
        throw new InvalidCastException("The Api does not implement the type " + typeof(IPage).Name);
    }
    private static Assembly GetAssembly(string trackerAssembly)
    {
        if (!string.IsNullOrEmpty(trackerAssembly))
        {
            string filename = trackerAssembly;
            string extension = Path.GetExtension(filename);
            if (!string.IsNullOrEmpty(extension) && !extension.Equals(".dll"))
                filename += ".dll";
            if (!File.Exists(filename))
            {
                string location = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                if (!string.IsNullOrEmpty(location))
                    filename = Path.Combine(location, "Apis", filename);
            }
            if (!File.Exists(filename))
                throw new FileNotFoundException("Could not find the file", filename); //Raise error? or log at least
            return Assembly.LoadFrom(filename);
        }
        return Assembly.GetExecutingAssembly();
    }
    private static object GetInstance(Type page)
    {
        if (!page.IsAssignableFrom(typeof(IPage)))
            throw new ArgumentException(string.Format("The type {0} does not implement the interface {1}.", page.Name, typeof(IPage).Name), "page");

        Type[] types = Type.EmptyTypes;

        var constructorInfo = page.GetConstructor(types);
        if (constructorInfo != null)
            return constructorInfo.Invoke(null);
        throw new ArgumentException(string.Format("The type {0} does not have a ctor that matches the ctor paramters.", page.Name), "page");
    }
}




Both project need to have a reference to the IPage interface. You can do this by creating a third project with the interface and reference this in both your other projects.

NB: They MUST use the same projects IPage. They should not have separate IPage interfaces that just look the same.

Hope that helps


这篇关于任何人都可以说如何使用一个项目的Dll作为另一个项目的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆