开发者

Pass enum value to method which is called by dynamic object

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.ExpandFu开发者_开发知识库ll)

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!


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.


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,

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜