如何让我的C#程序不同的表现取决于什么组件是present? [英] How can my C# program behave differently depending on what assemblies are present?

查看:107
本文介绍了如何让我的C#程序不同的表现取决于什么组件是present?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的Windows Azure中的Web应用程序自托管的Web应用程序中重用一些code逻辑。目前,我有code是这样的:

I want to reuse some code logic from a self-hosted web application in my Windows Azure web application. Currently I have code like this:

void myFunction( params )
{
   //environment-neutral code
}

我需要重写code,使程序的行为不同,这取决于是否是在Azure上:

I need to rewrite that code to make the program act differently depending on whether is is on Azure:

void myFunctionModified( params )
{
    if( onAzure() ) {
       //run Azure-specific code
    } else {
       //run non-Azure code
    }
}

是的,我知道虚函数,但我需要这样code,至少实例化对象的权利之前,我可以调用这些虚函数。

yes, I know about virtual functions, but I'll need such code for at least instantiating the right object before I can call those virtual functions.

现在的问题是实施 onAzure()我需要用的东西从一个程序集,这只是在Windows Azure上present(如<一个href="http://msdn.microsoft.com/en-us/library/microsoft.windowsazure.serviceruntime.roleenvironment.aspx"相对=nofollow> RoleEnvironment 为例)。因此,我不看我怎么可以实施 onAzure(),这样它不会崩溃,当外界Windows Azure的环境中运行。

The problem is to implement onAzure() I need to use stuff from an assembly that is only present on Windows Azure (like RoleEnvironment for example). So I don't see how I can implement onAzure() so that it doesn't crash when run outside Windows Azure environment.

如何让我的应用程序的行为不同,这取决于哪些组件是在系统上present?

How do I make my application act differently depending on what assemblies are present on the system?

推荐答案

如果您不能使用条件编译符号其他答案建议再...

If you can't use conditional compilation symbols as other answers have suggested then...

您可以尝试使用反射只存在于Azure中得到一个类型? 如果找不到类型,那么你不是在Azure上。

Could you try and get a type using reflection that only exists on Azure? If it can't find the type then you're not on Azure.

或者,如果你想下井组装的路线,那么你可以检查 AppDomain.CurrentDomain.GetAssemblies()为您的Azure程序集。

Or, if you want to go down the assemblies route, then you could check AppDomain.CurrentDomain.GetAssemblies() for your Azure assembly.

这篇关于如何让我的C#程序不同的表现取决于什么组件是present?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆