本文整理汇总了C#中System.Reflection.Emit.ModuleBuilder.DefineResource方法的典型用法代码示例。如果您正苦于以下问题:C# ModuleBuilder.DefineResource方法的具体用法?C# ModuleBuilder.DefineResource怎么用?C# ModuleBuilder.DefineResource使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Reflection.Emit.ModuleBuilder
的用法示例。
在下文中一共展示了ModuleBuilder.DefineResource方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AddResource
/// <summary>
/// Embeds a single resource into the assembly.
/// </summary>
void AddResource(ModuleBuilder moduleBuilder, ResourceFile resourceFile)
{
string fileName = resourceFile.FileName;
IResourceWriter resourceWriter = moduleBuilder.DefineResource(Path.GetFileName(fileName), resourceFile.Name, ResourceAttributes.Public);
string extension = Path.GetExtension(fileName).ToLowerInvariant();
if (extension == ".resources") {
ResourceReader resourceReader = new ResourceReader(fileName);
using (resourceReader) {
IDictionaryEnumerator enumerator = resourceReader.GetEnumerator();
while (enumerator.MoveNext()) {
string key = enumerator.Key as string;
Stream resourceStream = enumerator.Value as Stream;
if (resourceStream != null) {
BinaryReader reader = new BinaryReader(resourceStream);
MemoryStream stream = new MemoryStream();
byte[] bytes = reader.ReadBytes((int)resourceStream.Length);
stream.Write(bytes, 0, bytes.Length);
resourceWriter.AddResource(key, stream);
} else {
resourceWriter.AddResource(key, enumerator.Value);
}
}
}
} else {
resourceWriter.AddResource(resourceFile.Name, File.ReadAllBytes(fileName));
}
}
示例2: AddResourceFile
/// <summary>Enbdeds resource into assembly</summary>
/// <param name="builder"><see cref="ModuleBuilder"/> to embede resource in</param>
/// <param name="name">Name of the resource</param>
/// <param name="path">File to obtain resource from</param>
/// <param name="attributes">Defines resource visibility</param>
//DefineResource
// Exceptions:
// System.ArgumentException:
// name has been previously defined or if there is another file in the assembly
// named fileName.-or- The length of name is zero.-or- The length of fileName
// is zero.-or- fileName includes a path.
//
// System.ArgumentNullException:
// name or fileName is null.
//
// System.Security.SecurityException:
// The caller does not have the required permission.
//ResourceReader
// Exceptions:
// System.ArgumentException:
// The stream is not readable.
//
// System.ArgumentNullException:
// The stream parameter is null.
//
// System.IO.IOException:
// An I/O error has occurred while accessing stream.
//AddResource
// Exceptions:
// System.ArgumentNullException:
// The name parameter is null.
//ReadAllBytes
// Exceptions:
// System.ArgumentException:
// path is a zero-length string, contains only white space, or contains one
// or more invalid characters as defined by System.IO.Path.InvalidPathChars.
//
// System.ArgumentNullException:
// path is null.
//
// System.IO.PathTooLongException:
// The specified path, file name, or both exceed the system-defined maximum
// length. For example, on Windows-based platforms, paths must be less than
// 248 characters, and file names must be less than 260 characters.
//
// System.IO.DirectoryNotFoundException:
// The specified path is invalid (for example, it is on an unmapped drive).
//
// System.IO.IOException:
// An I/O error occurred while opening the file.
//
// System.UnauthorizedAccessException:
// path specified a file that is read-only.-or- This operation is not supported
// on the current platform.-or- path specified a directory.-or- The caller does
// not have the required permission.
//
// System.IO.FileNotFoundException:
// The file specified in path was not found.
//
// System.NotSupportedException:
// path is in an invalid format.
//
// System.Security.SecurityException:
// The caller does not have the required permission.
private void AddResourceFile(ModuleBuilder builder,string name, FullPath path, ResourceAttributes attributes) {
IResourceWriter rw = builder.DefineResource(path.FileName, name, attributes);
string ext = path.Extension.ToLower();
if(ext == ".resources") {
ResourceReader rr = new ResourceReader(path);
using(rr) {
System.Collections.IDictionaryEnumerator de = rr.GetEnumerator();
while(de.MoveNext()) {
string key = de.Key as string;
rw.AddResource(key, de.Value);
}
}
} else {
rw.AddResource(name, File.ReadAllBytes(path));
}
}
示例3: AddResource
/// <summary>
/// Embeds a single resource into the assembly.
/// </summary>
void AddResource(ModuleBuilder moduleBuilder, ResourceFile resourceFile)
{
string fileName = resourceFile.FileName;
string extension = Path.GetExtension(fileName).ToLowerInvariant();
if (extension == ".resources") {
string fullFileName = Path.GetFileName(fileName);
IResourceWriter resourceWriter = moduleBuilder.DefineResource(fullFileName, resourceFile.Name, ResourceAttributes.Public);
AddResources(resourceWriter, fileName);
} else {
moduleBuilder.DefineManifestResource(resourceFile.Name, new FileStream(fileName, FileMode.Open), ResourceAttributes.Public);
}
}