views:

162

answers:

3

hello.

I'm working on program which dynamically(in runtime) loads dlls.
For an example: Microsoft.AnalysisServices.dll.

In this dll we have this enum:
namespace Microsoft.AnalysisServices
{
[Flags]
public enum UpdateOptions
{
Default = 0,
ExpandFull = 1,
AlterDependents = 2,
}
}
and we also have this class Cube:
namespace Microsoft.AnalysisServices
{
public sealed class Cube : ...
{
public Cube(string name);
public Cube(string name, string id); ..
..
..
}
}

I dynamically load this dll and create object Cube. Than i call a method Cube.Update(). This method deploy Cube to SQL Analysis server. But if i want to call this method with parameters Cube.Update(UpdateOptions.ExpandFull) i get error, because method doesn't get appropriate parameter.

I have already tried this, but doesn't work:
dynamic updateOptions = AssemblyLoader.LoadStaticAssembly("Microsoft.AnalysisServices", "Microsoft.AnalysisServices.UpdateOptions");//my class for loading assembly
Array s = Enum.GetNames(updateOptions);
dynamic myEnumValue = s.GetValue(1);//1 = ExpandFull
dynamicCube.Update(myEnumValue);// == Cube.Update(UpdateOptions.ExpandFull)

I know that error is in parameter myEnumValue but i don't know how to get dynamically enum type from assembly and pass it to the method. Does anybody know the solution?

Thank you very much for answers and help!

A: 

All enums can be treated the same as their base type. In this case, the base type of UpdateOptions is int so you can simply pass thevalue 1 into dynamicCube.Update like this:

dynamicCube.Update(1)

You don't need to bother with the dynamic enum type bits.

Daniel Renshaw
i've tried this, but don't work. Even if i add refernece to assembly and try this i get error: `method has some invalid arguments`
A: 

i tried dynamicCube.Update(1), but didn't work.
Errors:
1: method has some invalid arguments
2: cannot convert from 'int' to 'Microsoft.AnalysisServices.UpdateOptions

A: 

Hello.

I found the solution. The code is like this:

dynamic assembly = AssemblyLoader.LoadStaticAssembly("Microsoft.AnalysisServices", Microsoft.AnalysisServices.UpdateOptions");
dynamic expandFull = (Enum)Enum.Parse(assembly, "ExpandFull", true);
dynamicCub.Update(expandFull);

and it works!

Regards,