以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 Dot NET,C#,ASP,VB 』  (http://bbs.xml.org.cn/list.asp?boardid=43)
----  C#中通过Assembly类访问程序集信息  (http://bbs.xml.org.cn/dispbbs.asp?boardid=43&rootid=&id=76242)


--  作者:卷积内核
--  发布时间:8/3/2009 2:26:00 PM

--  C#中通过Assembly类访问程序集信息
C#中通过Assembly类可以访问程序集信息.

1.允许访问给定程序集的元元素,包含可以加载和执行程序集的方法;

2.加载程序集:使用静态方法Assembly.Load(程序集名称)或Assembly.LoadFrom(程序集完整路径名);

3.属性:

FullName:程序集显示名称;

3.方法:

GetTypes():获取程序集中定义的类型。

TestAssembly.cs:

view plaincopy to clipboardprint?
using System;    using System.Reflection;  
     namespace Magci.Test.Reflection   
{        public class TestAssembly       
{            public static void Main()        
    {                //将程序集加载到运行过程中      
          Assembly  ass = Assembly.Load("TestCustomAttributes");       
         Assembly  ass1 = Assembly.LoadFrom(@"E:\CODE\dotNet\C#\9-Reflection\TestCustomAttributes.dll");        
        //获取程序集显示名称               
Console.WriteLine(ass1.FullName);      
             //获取程序集中定义的类型       
         Type[] types = ass.GetTypes();        
        foreach (Type t in types)   
             {                    Console.WriteLine(t.FullName);      
          }    
        }      
  }
    }  


--  作者:卷积内核
--  发布时间:8/3/2009 2:27:00 PM

--  
assembly中嵌入图片抓取器

从assembly中提取图片并显示在UI上的方法是主要LoadImagesFromAssembly.

private void LoadImagesFromAssembly( string assemblyPath )
{
// Try to load the assembly at the specified location.
   Assembly assembly = this.LoadAssembly( assemblyPath, true );
if( assembly == null )
return;
this.currentAssembly = assembly;
// Dispose of the images currently being displayed, if any.
   if( this.bindingSource.DataSource != null )
foreach( ImageInfo imgInfo in this.bindingSource.DataSource
as List<ImageInfo> )
imgInfo.Dispose();
// Bind to a list of every image embedded in the assembly.
   this.bindingSource.DataSource =
this.ExtractImagesFromAssembly( this.currentAssembly );
}

  如上, ImageGrabberForm 用BindingSource组件存储图片供数据绑定. BindingNavigator, DataGridView, PropertyGrid 和PictureBox都绑定到这一数据源,就可以相当简单地在各UI元素间保持一致.

  从assembly中提取图片的实际工作是在ExtractImagesFromAssembly 方法中:

private List<ImageInfo> ExtractImagesFromAssembly( Assembly assembly )
{
List<ImageInfo> imageInfos = new List<ImageInfo>();
foreach( string name in assembly.GetManifestResourceNames() )
{
using( Stream stream = assembly.GetManifestResourceStream( name ) )
{
// Treat the resource as an icon.
         try
{
Icon icon = new Icon( stream );
imageInfos.Add( new ImageInfo( icon, name ) );
continue;
}
catch( ArgumentException )
{
stream.Position = 0;
}
// Treat the resource as a cursor.
         try
{
Cursor cursor = new Cursor( stream );
imageInfos.Add( new ImageInfo( cursor, name ) );
continue;
}
catch( ArgumentException )
{
stream.Position = 0;
}
// Treat the resource as an image.
         try
{
Image image = Image.FromStream( stream );
// If the image is an animated GIF, do not add it to the
// collection because the Image class cannot handle them and
// will throw an exception when the image is displayed.
            FrameDimension frameDim =
new FrameDimension( image.FrameDimensionsList[0] );
bool isAnimatedGif = image.GetFrameCount( frameDim ) > 1;
if( !isAnimatedGif )
imageInfos.Add( new ImageInfo( image, name ) );
else
image.Dispose();
continue;
}
catch( ArgumentException )
{
stream.Position = 0;
}
// Treat the resource as a resource file.
         try
{
// The embedded resource in the stream is not an image, so
// read it into a ResourceReader and extract the values
// from there.
            using( IResourceReader reader = new ResourceReader( stream ) )
{
foreach( DictionaryEntry entry in reader )
{
if( entry.Value is Icon )
{
imageInfos.Add( new ImageInfo( entry.Value, name ) );
}
else if( entry.Value is Image )
{
imageInfos.Add( new ImageInfo( entry.Value, name ) );
}
else if( entry.Value is ImageListStreamer )
{
// Load an ImageList with the ImageListStreamer and
// store a reference to every image it contains.
                     using( ImageList imageList = new ImageList() )
{
imageList.ImageStream =
entry.Value as ImageListStreamer;
foreach( Image image in imageList.Images )
imageInfos.Add( new ImageInfo( image, name ) );
}
}
}
}
}
catch( Exception )
{
}
}
}
return imageInfos;
}

  上面的代码在assembly中为每个已命名资源都打开一个流,然后依次尝试从流中创建图标Icon, (失败的话创建)光标Cursor, (失败的话创建)图片Image, 全部失败的话通过System.Resources.ResourceReader读取内容. 这个resource reader可以把图片,图标和ImageList中的图片从资源文件.resx中提取出来. ImageInfo类是用来存取图片及其辅助信息的.


--  作者:卷积内核
--  发布时间:8/3/2009 2:28:00 PM

--  
c#中Assembly CreateInstance的使用问题

现在所有一个程序,需要使用C#的反射动态调用DLL文件,我目前只会使用DockContent FormLoad = (DockContent)Assembly.Load(an).CreateInstance(fn)【an是程序集的名称,fn是程序集中命名空间的名称】调用程序。
    但是,这样只能调用构造函数中没有参数的程序,可是如果构造参数中有函数(例如public baidu(string baidu)),调用的时候想要赋予构在函数中的参数参数值(baidu("baidu")),调用这个有参数的构造函数,而不是默认的没有参数的。
    这个应该怎么解决呢?


-------------------------------------------------------------------------------------------------------------------------

使用这个重载函数:C#
public Object CreateInstance (
string typeName,
bool ignoreCase,
BindingFlags bindingAttr,
Binder binder,
Object[] args,
CultureInfo culture,
Object[] activationAttributes
)
参数
typeName
要查找的类型的 Type.FullName。

ignoreCase
如果为 true,则忽略类型名的大小写;否则,为 false。

bindingAttr
影响执行搜索的方式的位屏蔽。此值是 BindingFlags 中的位标志的组合。

binder
一个启用绑定、参数类型强制、成员调用以及通过反射进行 MemberInfo 对象检索的对象。如果 binder 为空引用(在 Visual Basic 中为 Nothing),则使用默认联编程序。

args
Object 类型的数组,包含要传递给构造函数的参数。此参数数组在数量、顺序和类型方面必须与要调用的构造函数的参数匹配。如果需要默认的构造函数,则 args 必须是空数组或空引用(在 Visual Basic 中为 Nothing)。

culture
用于控制类型强制的 CultureInfo 的实例。如果这是空引用(在 Visual Basic 中为 Nothing),则使用当前线程的 CultureInfo。(例如,这对于将表示 1000 的 String 转换为 Double 值是必需的,因为不同的区域性以不同的方式表示 1000。)

activationAttributes
Object 类型的数组,包含一个或多个可以参与激活的激活属性。激活属性的一个示例是:

URLAttribute(http://hostname/appname/objectURI)


W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
78.125ms