`
tiw
  • 浏览: 25535 次
  • 性别: Icon_minigender_1
  • 来自: 西边
文章分类
社区版块
存档分类
最新评论

How to get Type

阅读更多

所有程序都是在mono环境下实验的

在.Net 中 Type 对于反射来说是一个至关重要的数据类型。在 .NET 中 有三种方法可以得到 Type,
  1. Object.GetType();
  2. System.Type.GetType();
  3. typeof();

方法一,已知一个对象,根据对象获得其 Type,例如:

c# 代码
 
  1. Book b = new Book();  
  2.   
  3. Type typeOfABook = b.GetType();  



方法二,根据类的名称,assembly的名称获得有关类的 Type, 例如一个Book类在Bookstore的namespace里,

c# 代码
 
  1. //------------- Book.cs ---------------//  
  2. namespace Bookstore{  
  3.     public class Book{  
  4.         ....  
  5.     }  
  6. }  


编译个文件到 mybook.dll 里面。 要动态的获得有关 Book 类的所有信息,可以

Type typeOfBook = Type.GetType("Bookstore.Book, mybook.dll", false, true);

* 要注意的是: mybook.dll 要保存在和可执行文件相同的目录下面, 我试过用下面的 .config 来添加寻找
assembly的路径,但是没有什么用处。

xml 代码
 
  1. <?xml version="1.0"?>  
  2. <configuration>  
  3.     <runtime>  
  4.         <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">  
  5.             <probing privatePath="modules"/>  
  6.         </assemblyBinding>  
  7.     </runtime>  
  8. </configuration>  


在msdn里有一段话,也许可以解释:

You can also make a dynamic reference to an assembly by providing the
calling method with only partial information about the assembly, such as
specifying only the assembly name. In this case, only the application
directory is searched for the assembly, and no other checking occurs.

大致意思是, 如果用的不是assembly的全名(assembly name, version, culture, and public key token)
则 .NET 只会在应用程序的目录里寻找相应的类。 这里不清楚的是对 "application directory" 的解释,
是指应用程序所在的目录, 还是包括这个目录的子目录? 不知道怎样可以打印出 .NET 的 "library path"。



方法三,类似方法二

c# 代码
 
  1. Type typeOfBook = typeof(Book);  


前提条件是 Book 所在的 assembly 必须在 GAC(Global Assembly Cache)中。
分享到:
评论

相关推荐

    How to get current user of sharepoint 2013

    type: "GET", headers: { "Accept": "application/json;odata=verbose" }, success: function(data) { var userName = data.d.Title; var email = data.d.Email; // 其他属性访问... console.log('用户名:' ...

    How to use SFTP

    You should ensure that the server's public keys are loaded by the client as described in How to use SFTP (with server validation - known hosts), or you may want to switch off server validation to get ...

    How to use postman

    5. **添加头部信息**:某些API可能需要特定的头部信息,如`Content-Type`或认证令牌等。这时可以在“Headers”标签页中添加这些头部信息。 6. **发送请求**:完成所有必要的配置后,点击“Send”按钮发送请求。 7. *...

    Get Programming - Learn to code with Python.epub

    LEARNING HOW TO PROGRAM Lesson 1 - Why should you learn how to program? Lesson 2 - Basic principles of learning a programming language UNIT 1 - VARIABLES, TYPES, EXPRESSIONS, AND STATEMENTS Lesson 3 ...

    How to write a Routine in DTP

    DATA: lv_fisc_year TYPE sy-datum FIELD-SYMBOLS, lv_fisc_period TYPE sy-datum FIELD-SYMBOLS&lt; &gt;. * 获取系统当前日期 CALL FUNCTION 'GET_CURRENT_DATE' EXPORTING calendar_type = 'GREGORIAN' IMPORTING ...

    Packt.Julia.High.Performance.2nd.Edition.rar

    Julia is a high-level, high-performance dynamic ... Toward the end, you will get a flavor of Julia's distributed computing capabilities and how to run Julia programs on a large distributed cluster.

    How to bind data to different DataTemplate in Universal Windows Platform apps

    public DataTemplate Template1 { get; set; } public DataTemplate Template2 { get; set; } protected override DataTemplate SelectTemplateCore(object item, DependencyObject container) { if (item is ...

    How-to-Get-an-Image-in-Java.rar_in

    BufferedImage resized = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB); Graphics2D g = resized.createGraphics(); g.drawImage(image, 0, 0, newWidth, newHeight, null); g.dispose...

    learning unreal engine game development

    learning how to create your own basic custom Material and how to use simple lights to light up the interior of the level. Chapter 5, Animation and AI, covers how animation works in Unreal Engine and ...

    howto-descriptor.pdf

    1. __get__(self, obj, type=None) -&gt; value:此方法返回描述器的值。obj通常是None或实例对象,而type通常是类对象。 2. __set__(self, obj, value) -&gt; None:此方法将值赋给描述器。在数据描述器中必须实现此方法...

    How to use httplib2 in Python 3

    在第一个请求中,由于服务器配置了默认的两小时缓存时间,所以任何后续对相同URI的GET请求都会从磁盘缓存中返回值,而不会再次向服务器发送请求。而在第二个请求中,我们通过添加`Cache-Control`头部并设置其值为`no...

    C# how to send http request

    这里,我们创建了一个`StringContent`对象,包含JSON格式的POST数据,并设置了合适的Content-Type头。 如果你正在处理的是`RegisterMerries`这样的场景,可能涉及到注册多个用户或批量操作。这时,你可能需要构建一...

    Lock in filter.rar_NOISE_dynamic signal_flicker noise_lock in_we

    The goal of this exercise is to study how the flicker noise can heavily distort these type of measurements and how the utilization of locking amplifiers can avoid this problem but with certain ...

    Learning Windows 8 Game Development

    you will learn how to write the code required to set everything up, get some graphics on screen, and then jump into the fun part of adding gameplay to turn a graphics sample into a proper game....

    Access 2007 VBA Programming For Dummies

    This gentle introduction to VBA programming covers everything you need to get started, including: Basic programming skills and concepts Explanations of modules, procedures, objects, and arguments ...

    How to Send SMS using Java Program

    1. **API调用**:创建HTTP请求对象,设置URL(通常是API提供商的URL)、HTTP方法(POST或GET)、请求头(如Content-Type、Authorization)和请求体(包含短信内容和接收者信息)。 2. **数据序列化**:如果API要求...

Global site tag (gtag.js) - Google Analytics