http://community.csdn.net/Expert/topic/3361/3361663.xml?temp=.267605

解决方案 »

  1.   

    Activator.CreateInstance(assemblyName, typeName);
      

  2.   

    估计得用反射什么,比如getType()之类的。查一下吧。
      

  3.   

    Assembly SampleAssembly;      
    SampleAssembly = Assembly.Load("程序集名称");
    程序集必须被引,如果不想引用可以LoadFilestring strClass = "clsMyPart";
    object myClass = SampleAssembly.CreateInstance(strClass );
    具体用的时候可以将myClass拆箱
      

  4.   

    如果是这样的意思可以通过ConstructorInfo来实现,具体参见msdn.
      

  5.   

    哥哥们 讲具体的啊 我搞不懂 !!
    我的类文件叫clsMyPart.cs  类叫clsMyPart,clsMyPart有公共方法 myFunction(),具体该怎么做啊???
      

  6.   

    Type a=Type.GetType("clsMyPart");
      

  7.   

    Type t = Type.GetType("XXX.clsMyPart",Version=1.0.3300.0, Culture=neutral, PublicKeyToken=XXXXXXXXXXXXXXXX");//类的全名+版本号+PublicKeyToken,如Type t = Type.GetType("System.Data.DataTable,System.Data,Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");clsMyPart table = (clsMyPart)Activator.CreateInstance(t);
      

  8.   


    strClass会得到"webpartFiles","webparDoc"等字符串,这些字符串正好是我的类
    //-----------------------------------
    for (int i = 0; i <= positionDS.Tables["tb_layout"].Rows.Count-1; i++)
    {
    int Iscomponent   = Int32.Parse(positionDS.Tables["tb_layout"].Rows[i]["intIscomponent"].ToString());
    string strClass  = positionDS.Tables["tb_layout"].Rows[i]["strCtrlSource"].ToString();if (Iscomponent == 0)
    {
    //这里是需要将字符串实例化成类的地方,上面的方法都没有成功

    }
    }-------------------------------------------------------------------------//下面是我的类文件  webpartFiles.cs
    using System;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;
    namespace contentManage
    {
    /// <summary>
    /// webpartQuery改编至webpartDoc
    /// </summary>
    [DefaultProperty("Text"), 
    ToolboxData("<{0}:webpart runat=server></{0}:webpart>")]
    public class webpartFiles: WebControl 
    {

    public webpartFiles(string strlay)
    {
    webpart myWebpart   = new webpart();
    myWebpart.leftTitleCss        = "fileTitleL";
    myWebpart.rightTitlweCss      = "fileTitleR";
    myWebpart.centerTitleCss      = "fileTitle";
    myWebpart.listBgCss           = "filebg";
    myWebpart.pointImg            = "<img src=\"images/ico_down.gif\" alt=\"download\" />"; myWebpart.titleLink           = "model_filesList.aspx?classid=";
    myWebpart.addLink             = "adminEditFiles.aspx?perclassid=";
    myWebpart.editLink            = "adminEditfiles.aspx?itemid=";
    myWebpart.viewLink            = "viewDocument.aspx?itemid="; myWebpart.TBidName            = "ItemID";
    myWebpart.TBTitleName         = "FileFriendlyName";
    myWebpart.TBWriterName        = "CreatedByUser";
    myWebpart.TBCreateTimeName    = "CreatedDate"; myWebpart.querySqlTableTop50  = " SELECT top 50 ItemID,ModuleID,FileFriendlyName,FileNameUrl,CreatedByUser,CreatedDate,Category,ContentSize,intCheck FROM tb_Documents ";
    myWebpart.querySqlTableTop5   = " SELECT top 5 ItemID,ModuleID,FileFriendlyName,FileNameUrl,CreatedByUser,CreatedDate,Category,ContentSize,intCheck FROM tb_Documents ";
    myWebpart.querySqlView        = " where ModuleID='"+strlay+"' and  intcheck=0  order by itemid desc ";
    myWebpart.querySqlEdit        = " where ModuleID='"+strlay+"' and (intcheck=0 or CreatedByUser ='"+HttpContext.Current.User.Identity.Name.ToString()+"') order by itemid desc";
    myWebpart.querySqlCheck       = " where ModuleID='"+strlay+"' order by itemid desc"; myWebpart.webparTitleBuild(strlay); this.Controls.Add(myWebpart);

    }

    }}
      

  9.   

    只是给你一条思考的线路,具体方法自已举一反三,好好查下MSDNAssembly asm=Assembly.LoadFile("your dll file");
    Type a=asm.GetType("type name")
      

  10.   

    用Assembly asm=Assembly.LoadFile(strPatch);会生成"试图加载格式错误的程序" 的错误,strPatch = "C:\Inetpub\wwwroot\newPortal\cs\clsMyPart.cs"用Type a=Type.GetType("clsMyPart");
    会生成"未将对象引用设置到对象的实例" 的错误!!各位!!再看看怎么回事啊? string strctrlSource = positionDS.Tables["tb_layout"].Rows[i]["strCtrlSource"].ToString();//strctrlSource会取得不同的类名,这些类都是我定义好的类,都是.cs文件,我怎么样才能将 strctrlSource转化成类啊?