查询属性 - 字段属性

6.3.3 字段属性
None.gif using  System;
None.gif
using  System.Reflection;
None.gif
None.gif
namespace  FieldAttribs
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public enum RegHives
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        HKEY_CLASS_ROOT,
InBlock.gif        HKEY_CURRENT_USER,
InBlock.gif        HKEY_LOCAL_MACHINE,
InBlock.gif        HKEY_CURRENT_CONFIG
ExpandedSubBlockEnd.gif    }

InBlock.gif    
InBlock.gif    
public class RegKeyAttribute : Attribute
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public RegKeyAttribute(RegHives Hive, String ValueName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.Hive = Hive;
InBlock.gif            
this.ValueName = ValueName;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
protected RegHives hive;
InBlock.gif        
public RegHives Hive
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gif{return hive;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{hive = value;}
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
protected String valueName;
InBlock.gif        
InBlock.gif        
public String ValueName
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn valueName; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ valueName = value; }
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
InBlock.gif    
class SomeClass
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        [RegKey(RegHives.HKEY_CURRENT_USER, 
"Foo")]
InBlock.gif        
public int Foo;
InBlock.gif        
InBlock.gif        
public int Bar;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
InBlock.gif    
class Test
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        [STAThread]
InBlock.gif        
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Type type 
= Type.GetType("FieldAttribs.SomeClass");
InBlock.gif            
foreach(FieldInfo field in type.GetFields())
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
foreach(Attribute attr in field.GetCustomAttributes(true))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    RegKeyAttribute rka 
= attr as RegKeyAttribute;
InBlock.gif                    
if (null != rka)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        Console.WriteLine(
"{0} will be saved in " + "{1} \\\\ {2}"
InBlock.gif                                                        field.Name,
InBlock.gif                                                        rka.Hive,
InBlock.gif                                                        rka.ValueName);
InBlock.gif                    
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}


这个例子跟前一个方法属性的例子类似,就不仔细分析了。
只是注意,与MethodInfo对象从类型对象中获取方法信息一样,FieldInfo对象用于从类型信息中获取字段信息。
PS:
在打代码的时候,总是把 field打成 filed,以后注意。
输出的时候有两个warning:
warning CS0649 从未对字段“FieldAttribs.SomeClass.Foo”赋值,字段将一直保持其默认值 0
warning CS0649 从未对字段“FieldAttribs.SomeClass.Bar”赋值,字段将一直保持其默认值 0

当然这个例子只是用于演示目的,不用管它
最后执行输出:
Foo will be saved in HKEY_CURRENT_USER \\ Foo
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值