सबसे सुरक्षित तरीका है (यानी सबसे आसान तरीका है अपने आवेदन में एक गलती नहीं बनाने के लिए) के रूप में हो सकता है।
एक अंतरफलक जो उदाहरण के लिए, TFS के आपके उपयोग abstracts करें:
class Tfs : ITfs
{
public bool checkout(string filename)
{
... code here which uses the TFS assembly ...
}
}
अन्य वर्ग जो का उपयोग किए बिना इस इंटरफेस को लागू करता है लिखें:
interface ITfs
{
bool checkout(string filename);
}
एक वर्ग जो इस इंटरफेस TFS का उपयोग कर लागू करता लिखें टीएफएस:
class NoTfs : ITfs
{
public bool checkout(string filename)
{
//TFS not installed so checking out is impossible
return false;
}
}
कहीं सिंगलटन है:
static class TfsFactory
{
public static ITfs instance;
static TfsFactory()
{
... code here to set the instance
either to an instance of the Tfs class
or to an instance of the NoTfs class ...
}
}
अब सिर्फ एक ही जगह है, जो सावधान रहने की जरूरत है है (अर्थात TfsFactory निर्माता); आपका शेष कोड यह जानने के बिना कि TFS स्थापित है या नहीं, आपके TfsFactory.instance के ITfs विधियों का आह्वान कर सकता है।
नीचे हाल की टिप्पणियों का जवाब करने के लिए:
मेरी परीक्षण के अनुसार (मैं नहीं जानता कि यह है 'परिभाषित व्यवहार' है या नहीं) एक अपवाद फेंक दिया जाता है जब (जैसे ही) यदि आप एक विधि कॉल जो लापता असेंबली पर निर्भर करता है। इसलिए आपके असेंबली में कम-से-कम एक अलग विधि (या एक अलग वर्ग) में आपके कोड-जो-निर्भर-पर-अनुपलब्ध-असेंबली को समाहित करना महत्वपूर्ण है।
उदाहरण के लिए, निम्नलिखित यदि टॉक विधानसभा याद आ रही है लोड नहीं होगा:
using System;
using OptionalLibrary;
namespace TestReferences
{
class MainClass
{
public static void Main(string[] args)
{
if (args.Length > 0 && args[0] == "1") {
Talk talk = new Talk();
Console.WriteLine(talk.sayHello() + " " + talk.sayWorld() + "!");
} else {
Console.WriteLine("2 Hello World!");
}
}
}
}
निम्नलिखित इच्छा लोड:
using System;
using OptionalLibrary;
namespace TestReferences
{
class MainClass
{
public static void Main(string[] args)
{
if (args.Length > 0 && args[0] == "1") {
foo();
} else {
Console.WriteLine("2 Hello World!");
}
}
static void foo()
{
Talk talk = new Talk();
Console.WriteLine(talk.sayHello() + " " + talk.sayWorld() + "!");
}
}
}
ये परीक्षण के परिणाम (MSVC# 2010 का उपयोग कर रहे हैं और विंडोज़ पर .NET):
C:\github\TestReferences\TestReferences\TestReferences\bin\Debug>TestReferences.exe
2 Hello World!
C:\github\TestReferences\TestReferences\TestReferences\bin\Debug>TestReferences.exe 1
Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'OptionalLibrary, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
at TestReferences.MainClass.foo()
at TestReferences.MainClass.Main(String[] args) in C:\github\TestReferences\TestReferences\TestReferences\Program.cs:
line 11
C:\github\TestReferences\TestReferences\TestReferences\bin\Debug>
इस मामले में "टीएफएस" "टीम फाउंडेशन सिस्टम" है, है ना? –
हां, लेकिन मैं इस सवाल को टीएफएस के बारे में कम मानता हूं और असेंबली को संदर्भित करने का सबसे अच्छा तरीका है जो कुछ उपयोगकर्ता मशीनों पर मौजूद नहीं हो सकता है। – Justin
डी ओह, मैंने पहले वाक्य को ध्यान से पर्याप्त रूप से नहीं पढ़ा, इसलिए मुझे एहसास नहीं हुआ कि 'वैकल्पिक' से आपका मतलब है 'उपयोगकर्ता की मशीन पर मौजूद नहीं हो सकता है।' –