本文整理匯總了C#中System.BadImageFormatException類的典型用法代碼示例。如果您正苦於以下問題:C# BadImageFormatException類的具體用法?C# BadImageFormatException怎麽用?C# BadImageFormatException使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
BadImageFormatException類屬於System命名空間,在下文中一共展示了BadImageFormatException類的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: if
// Windows DLL (non-.NET assembly)
string filePath = Environment.ExpandEnvironmentVariables("%windir%");
if (! filePath.Trim().EndsWith(@"\"))
filePath += @"\";
filePath += @"System32\Kernel32.dll";
try {
Assembly assem = Assembly.LoadFile(filePath);
}
catch (BadImageFormatException e) {
Console.WriteLine("Unable to load {0}.", filePath);
Console.WriteLine(e.Message.Substring(0,
e.Message.IndexOf(".") + 1));
}
// The example displays an error message like the following:
// Unable to load C:\WINDOWS\System32\Kernel32.dll.
// The module was expected to contain an assembly manifest.
示例2: ToProperCase
//引入命名空間
using System;
public class StringLib
{
private string[] exceptionList = { "a", "an", "the", "in", "on", "of" };
private char[] separators = { ' ' };
public string ToProperCase(string title)
{
bool isException = false;
string[] words = title.Split( separators, StringSplitOptions.RemoveEmptyEntries);
string[] newWords = new string[words.Length];
for (int ctr = 0; ctr <= words.Length - 1; ctr++)
{
isException = false;
foreach (string exception in exceptionList)
{
if (words[ctr].Equals(exception) && ctr > 0)
{
isException = true;
break;
}
}
if (! isException)
newWords[ctr] = words[ctr].Substring(0, 1).ToUpper() + words[ctr].Substring(1);
else
newWords[ctr] = words[ctr];
}
return String.Join(" ", newWords);
}
}
輸出:
Unhandled Exception: System.BadImageFormatException: The format of the file 'StringLib.dll' is invalid.
示例3: Main
//引入命名空間
using System;
using System.Reflection;
public class Example
{
public static void Main()
{
string title = "a tale of two cities";
// object[] args = { title}
// Load assembly containing StateInfo type.
Assembly assem = Assembly.LoadFrom(@".\StringLib.dll");
// Get type representing StateInfo class.
Type stateInfoType = assem.GetType("StringLib");
// Get Display method.
MethodInfo mi = stateInfoType.GetMethod("ToProperCase");
// Call the Display method.
string properTitle = (string) mi.Invoke(null, new object[] { title } );
Console.WriteLine(properTitle);
}
}
示例4: Main
//引入命名空間
using System;
using System.IO;
using System.Reflection;
public class Example
{
public static void Main()
{
String[] args = Environment.GetCommandLineArgs();
if (args.Length == 1) {
Console.WriteLine("\nSyntax: PlatformInfo <filename>\n");
return;
}
Console.WriteLine();
// Loop through files and display information about their platform.
for (int ctr = 1; ctr < args.Length; ctr++) {
string fn = args[ctr];
if (! File.Exists(fn)) {
Console.WriteLine("File: {0}", fn);
Console.WriteLine("The file does not exist.\n");
}
else {
try {
AssemblyName an = AssemblyName.GetAssemblyName(fn);
Console.WriteLine("Assembly: {0}", an.Name);
if (an.ProcessorArchitecture == ProcessorArchitecture.MSIL)
Console.WriteLine("Architecture: AnyCPU");
else
Console.WriteLine("Architecture: {0}", an.ProcessorArchitecture);
Console.WriteLine();
}
catch (BadImageFormatException) {
Console.WriteLine("File: {0}", fn);
Console.WriteLine("Not a valid assembly.\n");
}
}
}
}
}