0

I have an assembly that contains the class RD_ToBeProcessed which inherits from ToBeProcessed. The classes are in separate assemblies.

I load an object using createInstance and then attempt to cast it with the following code:

    private Type tbpType = null;
    public ToBeProcessed getToBeProcessedObject(string data)
    {
        // The data is passed in so that the fields are populated with the
        // correct data.
        if (tbpType==null){
            Assembly assembly = 
                Assembly.LoadFrom("c:\\project\\RD_ToBeProcessed.dll");
            tbpType = assembly.GetType("myNameSpace.RD_ToBeProcessed");
        }
        Object tbp = Activator.CreateInstance(tbpType,data);
                    // This line throws an error
        return (ToBeProcessed)tbp;
    }

This is a repeat of the question .NET: Unable to cast object to interface it implements but I don't know how to resolve it.

The error thrown is

Unable to cast object of type 'myNameSpace.RD_ToBeProcessed' to type 'myNameSpace.ToBeProcessed'.

The accepted answer indicated that the problem was 2 different versions of the base assembly. But I have used ILSpy and both the ToBeProcessed dlls in the application directory and the one in the same directory as RD_ToBeProcessed report:

ToBeProcessed, Version=1.0.4336.31676, Culture=neutral, PublicKeyToken=null

So I am not sure what I am doing wrong. Should I change ToBeProcessed to be an interface (ItoBeProcessed) that is used in the app and the plugin? Then have a separate assembly that holds the base ToBeProcessed class which would not be referenced by the application at all (just the by plugin)?

EDIT: The problem was resolved by using an interface class. I still don't know what was going wrong but Kol's answer showed that in theory this should have worked correctly the way it was.

Community
  • 1
  • 1
CarbonMan
  • 4,350
  • 12
  • 54
  • 75

3 Answers3

1

some people asked it before have look to this question in stackoverflow How to get a Static property with Reflection

Community
  • 1
  • 1
DeveloperX
  • 4,633
  • 17
  • 22
1

The following solution compiles and runs without error:

Assembly #1: ToBeProcessed

Compiled to DLL, which is copied to c:\project and c:\project\test. Refers to System.dll. ToBeProcessed.cs:

using System;
using System.Reflection;

[assembly: AssemblyVersion("1.0.*")]

namespace myNameSpace
{
  public class ToBeProcessed
  {
    protected string data;
    public ToBeProcessed() { }
    public string Process() { return data.ToUpper(); }
  }
}

Assembly #2: RD_ToBeProcessed

Compiled to DLL, which is copied to c:\project. Refers to System.dll and ToBeProcessed.dll. RD_ToBeProcessed.cs:

using System;
using System.Reflection;

[assembly: AssemblyVersion("1.0.*")]

namespace myNameSpace
{
  public class RD_ToBeProcessed : ToBeProcessed
  {
    public RD_ToBeProcessed(string data) { this.data = data; }
  }
}

Assembly #3: ToBeProcessedTest

Compiled to EXE, which is copied to c:\project\test. Refers to System.dll and ToBeProcessed.dll. ToBeProcessedTest.cs:

using System;
using System.Reflection;

[assembly: AssemblyVersion("1.0.*")]

namespace myNameSpace
{
  class ToBeProcessedTest
  {
    private Type tbpType = null;
    public ToBeProcessed getToBeProcessedObject(string data)
    {
      if (tbpType == null)
      {
        Assembly assembly = Assembly.LoadFrom("c:\\project\\RD_ToBeProcessed.dll");
        tbpType = assembly.GetType("myNameSpace.RD_ToBeProcessed");
      }
      Object tbp = Activator.CreateInstance(tbpType, data);
      return (ToBeProcessed)tbp;
    }

    public static void Main()
    {
      ToBeProcessedTest test = new ToBeProcessedTest();
      ToBeProcessed tbp1 = test.getToBeProcessedObject("myData1");
      Console.WriteLine(tbp1.Process());
      ToBeProcessed tbp2 = test.getToBeProcessedObject("myData2");
      Console.WriteLine(tbp2.Process());
      Console.ReadKey(true);
    }
  }
}

Output:

MYDATA1
MYDATA2
kol
  • 27,881
  • 12
  • 83
  • 120
  • Well there is no doubt that you have covered the problem. Very impressed. The only thing that I can think that might be causing an issue is that I call some static methods in ToBeProcessed prior to creating an instance of RD_ToBeProcessed. Perhaps that causes issues?? – CarbonMan Nov 15 '11 at 12:34
  • Intuitively, no. To be sure, I tested this by adding a static SetParentheses method to ToBeProcessed, and calling it before creating tbp1 and tbp2. The code compiled and ran without problem. – kol Nov 15 '11 at 12:49
  • Even though I only resolved my problem by other means, I had to accept your answer because you proved it should have worked the way it was. – CarbonMan Nov 16 '11 at 01:41
0

check this post it explains reflection with examples. like case of inheritance and how to use type.GetMethods (BindingFlags.LookupAll) to get all methods.

SShebly
  • 2,043
  • 1
  • 21
  • 29