Coffeehouse Thread

8 posts

Type.GetType returning null

Back to Forum: Coffeehouse
  • User profile image
    trisonics

    I have a windows forms project in VS 2003 which I migrated to VS 2005 after migration I found out that the following code does not work after migration

    using System;

    using System.Drawing;

    using System.Collections;

    using System.ComponentModel;

    using System.Windows.Forms;

    using System.Data;

    using System.Reflection;

    ......

    ......

    private void OpenForm(string strName)

    {

    ....

    string strModName =  "FX.FORM.UI.REPORT.FRMRPT"; // this just an eample the value is fetched from the database

    Form objCurrFrm = (Form) Activator.CreateInstance(Type.GetType(strModName)); <--fails

    objCurrFrm.GetType().InvokeMember("Show",BindingFlags.Default |BindingFlags.InvokeMethod,null,objCurrFrm,new object[]{});

    ....

    }

    ......
    .....

    this is becaue Type.GetType is returning null.

    I tried using the Assembly.GetTyp but I am getting an error as "Cannot load file or assembly ... or one of its dependencies"

    But if I directly do

    Form objCurrFrm = new FX.FORM.UI.REPORT.FRMRPT();

    It works fine.

    Iin my solution I have a login form which is a seperate project. This project has ref. to the main project which biuld the menu etc and rest of each are seperate project like sub project of the main all this individual project are being ref. by the main project.

    After the login in successful the main project is loaded. This has the menu which is built from the database the dll names are fetched from the database. Once the menu is clicked the corrosponding dll has to be loaded.


    Does anyone has any idea how to solve it.

     

  • User profile image
    littleguru

    You need to use the string that is returned if you do the following:

    string typeName = typeof(FX.FORM.UI.REPORT.FRMRPT()).AssemblyQualifiedName;

    Not that you need to add this call to your program. Just use the string that is returned by AssemblyQualifiedName. The string contains the assembly, where the type is found in. They have changed that from .NET 1.1 to .NET 2.0.

  • User profile image
    Rotem Kirshenbaum

    Can you save the type name with the assembly name in the DB ?

  • User profile image
    trisonics

    typeof cannot be used with a variable it will throw complile time error i.e. I cannot do

    typeof (strModName).AssemblyQualifiedName

    I donot know the assembly name untill runtime.

    if Report Screen need to be opened the "FX.FORM.UI.REPORT.FRMRPT" is to be called if Chart Screen is to be opened "FX.FROM.UI.CHART.FRMCHART" needs to be called and so on.

    So I cannot hardcode the name for calling the typeof function.

  • User profile image
    trisonics

    No

  • User profile image
    littleguru

    trisonics wrote:
    typeof cannot be used with a variable it will throw complile time error i.e. I cannot do

    typeof (strModName).AssemblyQualifiedName

    I donot know the assembly name untill runtime.

    if Report Screen need to be opened the "FX.FORM.UI.REPORT.FRMRPT" is to be called if Chart Screen is to be opened "FX.FROM.UI.CHART.FRMCHART" needs to be called and so on.

    So I cannot hardcode the name for calling the typeof function.



    I dunno what part of my post you didn't read! But get the assembly qualified string and use that one! Create a test application where you get that string and use that in your real application. I even made it clear that you only get the string and use it from then on, not that you embed that typeof(...) into your application...

    And of an instance you get the type by calling GetType().

    Somewhere you have the strings of the types right now. Replace them with the strings that are returned by AssemblyQualifiedName.

  • User profile image
    vivsriaus

    I did this, yet I'm getting the "null" exception.

    Type called = Type.GetType(sNamespace + "." + sClass + "," + sAssembly, true);           

    I pass in the correct Assembly, Namespace and Class names, yet I get the error. However, when I move the class to a different assembly, I don't see that exception. Is there any other catch to this? I'm really not sure where I'm going wrong!

  • User profile image
    JChung2006

    Is your assembly GAC'ed?

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.