新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   XML论坛     W3CHINA.ORG讨论区     计算机科学论坛     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> 本版讨论.NET,C#,ASP,VB技术
    [返回] 中文XML论坛 - 专业的XML技术讨论区计算机技术与应用『 Dot NET,C#,ASP,VB 』 → C#中通过Assembly类访问程序集信息 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 6072 个阅读者  浏览上一篇主题  刷新本主题   树形显示贴子 浏览下一篇主题
     * 贴子主题: C#中通过Assembly类访问程序集信息 举报  打印  推荐  IE收藏夹 
       本主题类别:     
     卷积内核 帅哥哟,离线,有人找我吗?
      
      
      威望:8
      头衔:总统
      等级:博士二年级(版主)
      文章:3942
      积分:27590
      门派:XML.ORG.CN
      注册:2004/7/21

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给卷积内核发送一个短消息 把卷积内核加入好友 查看卷积内核的个人资料 搜索卷积内核在『 Dot NET,C#,ASP,VB 』的所有贴子 访问卷积内核的主页 引用回复这个贴子 回复这个贴子 查看卷积内核的博客楼主
    发贴心情 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);      
              }    
            }      
      }
        }


       收藏   分享  
    顶(0)
      




    ----------------------------------------------
    事业是国家的,荣誉是单位的,成绩是领导的,工资是老婆的,财产是孩子的,错误是自己的。

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2009/8/3 14:26:00
     
     卷积内核 帅哥哟,离线,有人找我吗?
      
      
      威望:8
      头衔:总统
      等级:博士二年级(版主)
      文章:3942
      积分:27590
      门派:XML.ORG.CN
      注册:2004/7/21

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给卷积内核发送一个短消息 把卷积内核加入好友 查看卷积内核的个人资料 搜索卷积内核在『 Dot NET,C#,ASP,VB 』的所有贴子 访问卷积内核的主页 引用回复这个贴子 回复这个贴子 查看卷积内核的博客2
    发贴心情 
    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类是用来存取图片及其辅助信息的.

    ----------------------------------------------
    事业是国家的,荣誉是单位的,成绩是领导的,工资是老婆的,财产是孩子的,错误是自己的。

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2009/8/3 14:27:00
     
     卷积内核 帅哥哟,离线,有人找我吗?
      
      
      威望:8
      头衔:总统
      等级:博士二年级(版主)
      文章:3942
      积分:27590
      门派:XML.ORG.CN
      注册:2004/7/21

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给卷积内核发送一个短消息 把卷积内核加入好友 查看卷积内核的个人资料 搜索卷积内核在『 Dot NET,C#,ASP,VB 』的所有贴子 访问卷积内核的主页 引用回复这个贴子 回复这个贴子 查看卷积内核的博客3
    发贴心情 
    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)

    ----------------------------------------------
    事业是国家的,荣誉是单位的,成绩是领导的,工资是老婆的,财产是孩子的,错误是自己的。

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2009/8/3 14:28:00
     
     GoogleAdSense
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 Dot NET,C#,ASP,VB 』的所有贴子 访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2025/6/20 19:02:41

    本主题贴数3,分页: [1]

    管理选项修改tag | 锁定 | 解锁 | 提升 | 删除 | 移动 | 固顶 | 总固顶 | 奖励 | 惩罚 | 发布公告
    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    10,921.880ms