将枚举值传递给动态对象调用的方法
我正在开发动态(在运行时)加载 dll 的程序。
例如:Microsoft.AnalysisServices.dll
。
在这个 dll 中,我们有这个枚举:
namespace Microsoft.AnalysisServices
{
[Flags]
public enum UpdateOptions
{
Default = 0,
ExpandFull = 1,
AlterDependents = 2,
}
}
并且我们还有这个类 Cube
:
namespace Microsoft.AnalysisServices
{
public sealed class Cube : ...
{
public Cube(string name);
public Cube(string name, string id);
..
..
..
}
}
我动态加载这个 dll 并创建对象Cube
。然后我调用了一个方法Cube.Update()
。此方法将Cube部署到SQL分析服务器。但如果我想打电话 此方法带有参数 Cube.Update(UpdateOptions.ExpandFull)
我收到错误,因为方法没有获取适当的参数。
我已经尝试过这个,但不起作用:
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)
我知道参数 myEnumValue
中存在错误,但我不知道如何从程序集中动态获取枚举类型并将其传递给方法。有人知道解决办法吗?
非常感谢您的解答和帮助!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
所有枚举都可以像其基本类型一样对待。在本例中,UpdateOptions 的基本类型是 int,因此您可以简单地将值 1 传递到dynamicCube.Update 中,如下所示:
您无需担心动态枚举类型位。
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:
You don't need to bother with the dynamic enum type bits.
我找到了解决方案。代码如下:
动态程序集 = AssemblyLoader.LoadStaticAssembly("Microsoft.AnalysisServices", Microsoft.AnalysisServices.UpdateOptions");
动态expandFull = (Enum)Enum.Parse(程序集, "ExpandFull", true);
dynamicCub.Update(expandFull);
并且它有效!
问候,
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,