0

I am trying to load the old version of farpoint dll in my project by using below code

     System.Reflection.Assembly assembly = System.Reflection.Assembly.LoadFile(@"FarPoint.Web.Spread.dll");
    System.Type MyDLLFormType = assembly.GetType("FarPoint.Web.Spread.FpSpread");
    var c = Activator.CreateInstance(MyDLLFormType);

The problem is after the instance is created, all the available methods of farpoint are not available [ for example - if i create the object directly the methods like saveExcel or savechanges are available with the instance]

                FpSpread fpProxyObject = new FpSpread();
                fpProxyObject.SaveExcel();
Code's
  • 208
  • 2
  • 18

2 Answers2

1

They are available, just not at compile time. Activator.CreateInstance() returns an object. You could of course cast the object:

var c = Activator.CreateInstance(...)
FpSpread fpProxyObject = (FpSpread)c;

But that would probably beat the whole purpose of using reflection to create the instance.

You can access all members of the result object by using reflection, ie:

MethodInfo saveExcelMethod = c.GetType().GetMethod("SaveExcel");
if (saveExcelMethod == null) throw new ApplicationException("Incorrect version of FarPoint");
saveExcelMethod.Invoke(c);
C.Evenhuis
  • 25,996
  • 2
  • 58
  • 72
  • I cannot cast it to Farpoint object - the reason is I am having 2 versions of Farpoint dll - The latest one I am using in my project and older version I am referring. So if I typecast to Farpoint - it automatically converted it to the latest version - but I need old version – Code's Aug 04 '15 at 17:21
  • Add both references, with different aliases. – Adriano Repetti Aug 04 '15 at 17:32
1

Intellisense is not working, because, as said @C.Evenhuis, Activator.CreateInstance returns object, so you should cast it to appropriate type.

If type is not known at compile time, but you have access to a code-base, you could try to add interface for it, and implement it by your class. Then cast object to that interface and use it. (I don't know your purpose, but interface could be treated as a contract for all the types, that you will load dynamically).

If type is not known at compile time and you have no access to a code-base, you could use reflection for method invocation or use dynamic instead.

dynamic c = Activator.CreateInstance(MyDLLFormType);
c.SaveExcel(); // this method invocation will be bound in runtime.

By the way be carefull, while using Assembly.LoadFile. You may get more details from this article.

Uladzislaŭ
  • 1,680
  • 10
  • 13
  • I am getting below error message when I used dynamic - Could not load file or assembly 'FarPoint.Web.Chart, Version=5.0.3515.2008, Culture=neutral or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. – Code's Aug 05 '15 at 05:02
  • @Coder2014, as exception messaeg says your loaded assembly depends on `FarPoint.Web.Chart`. So you should load it as well, because `LoadFile` won't resolve dependencies of loading assembly automagically. Quick googling gave me that [link](http://stackoverflow.com/questions/22967928/loading-dependent-assemblies-manually), which describes similar problem. (Before adding one more queistion to this site, please, try to resolve your problem yourself. Try hard.) – Uladzislaŭ Aug 07 '15 at 08:18