나는 다른 누군가가 작성한 dll을 가지고 있으며 많은 결함이 있습니다. 이 DLL에 클래스가 하나만 정의되어 있다고 가정 해 봅시다. 이 DLL을 가져와이 클래스의 인스턴스를 만들어야합니다. DLL을 코드는 다음과 같을 수 있습니다 동적으로로드 한 DLL이 버그로 인해 프로그램이 다운되는 것을 피하는 방법
[Serializable()]
public class makeExceptionClass
{
public bool isStringNormalized(string aString)
{
// A null check should be performed
return aString.IsNormalized();
}
}
나는 내 프로그램은 여전히 경우에도 DLL 충돌을 실행할 수 있는지 여부를 확인하는 작은 프로그램을 썼다. 이 프로그램은 단순한 개념 증명 일뿐입니다. 두 개의 인수를 취합니다. 첫 번째 인수는 어셈블리에서 DLL을 직접로드하는 데 사용되고 두 번째 인수는 충돌을 유발하는 데 사용됩니다. 프로그램의 코드는 다음
class Program
{
[PermissionSetAttribute(SecurityAction.Demand, Name = "FullTrust")]
static void Main(string[] args)
{
string typeOfLoading = args[0];
string crash = args[1];
// Load the DLL
if (typeOfLoading.Equals("direct") == true)
{
Console.WriteLine("Loading directly a DLL");
Assembly anAssembly = Assembly.Load("unloadableDLL"); // Directly load the DLL
unloadableDLL.makeExceptionClass anObject = (unloadableDLL.makeExceptionClass)anAssembly.CreateInstance("unloadableDLL.makeExceptionClass");
if (crash.Equals("crash") == true)
{
bool test = anObject.isStringNormalized(null);
}
else
{
bool test = anObject.isStringNormalized("test");
}
}
else if (typeOfLoading.Equals("indirect") == true)
{
Console.WriteLine("Loading indirectly a DLL");
AppDomain anAppDomain = AppDomain.CreateDomain("RemoteLoaderDomain"); // Assume it does not fail;
Type t = typeof(unloadableDLL.makeExceptionClass);
unloadableDLL.makeExceptionClass anObject = (unloadableDLL.makeExceptionClass)anAppDomain.CreateInstanceAndUnwrap("unloadableDLL", t.FullName);
if (crash.Equals("crash") == true)
{
bool test = anObject.isStringNormalized(null);
}
else
{
bool test = anObject.isStringNormalized("test");
}
Console.WriteLine("Unloading the domain");
AppDomain.Unload(anAppDomain);
}
else
{
// don't care
}
// terminate
Console.WriteLine("Press any key to exit");
Console.ReadKey();
}
}
문제는 DLL이 직접 또는 응용 프로그램 도메인에로드되어있는 경우 내 프로그램에 관계없이 충돌한다는 것입니다. 저는 C#에 완전히 익숙했지만 (오늘 아침에 시작되었습니다) C++ 배경을 가지고 있습니다. 전화 주변
? 예외? – BlueM
MEF 및/또는 MAF 조사를 고려해보십시오. 둘 다 잠재적 인 적대적 또는 버그가있는 제 3 자 코드를 응용 프로그램에로드하고 기존 인터페이스를 통해 통신 할 수있게 해주는 프레임 워크입니다. 보다 일반적으로 : 당신은 본질적으로 어렵고 숙련 된 C# 프로그래머조차도 까다로운 작업을 시도하고 있습니다. 누군가의 C# 코딩 첫날 또는 심지어 1000 번째 날로 선택한 것이 아닙니다. 행운을 빕니다! –
그래, 나는 그것을 정말로 선택하지 않았다, 나는 나의 상사에 의해 그것을 친절히 요청 받았다 : D –