从dataReader到Entity转化时利用Reflect示例(解决OutOfRangeException错误)(downmoon)

在生成代码时经常需要从DataReader转化为Entity

比如

ContractedBlock.gif ExpandedBlockStart.gif Product #region    Product
InBlock.gif        
internal    static    Downmoon.Product.Framework.Components.Product    LoadSingleProduct(IDataReader reader)    
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{            Downmoon.Product.Framework.Components.Product    m_Product=    new    Downmoon.Product.Framework.Components.Product();
InBlock.gif       
ExpandedSubBlockStart.gifContractedSubBlock.gif                
if(reader["P_ID"]!= DBNull.Value)    dot.gif{
InBlock.gif                    m_Product.P_ID
=Convert.ToInt64(reader["P_ID"]);
ExpandedSubBlockEnd.gif                }

InBlock.gif     
ExpandedSubBlockStart.gifContractedSubBlock.gif                
if(reader["P_Name"]!= DBNull.Value)    dot.gif{
InBlock.gif                    m_Product.P_Name
=Convert.ToString(reader["P_Name"]);
ExpandedSubBlockEnd.gif                }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif                
if(reader["P_SingleIntro"]!= DBNull.Value)    dot.gif{
InBlock.gif                    m_Product.P_SingleIntro
=Convert.ToString(reader["P_SingleIntro"]);
ExpandedSubBlockEnd.gif                }

InBlock.gif        
InBlock.gif           
ExpandedSubBlockStart.gifContractedSubBlock.gif                
if(reader["P_Intro"]!= DBNull.Value)    dot.gif{
InBlock.gif                    m_Product.P_Intro
=Convert.ToString(reader["P_Intro"]);
ExpandedSubBlockEnd.gif                }

InBlock.gif          
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**///***************************** 
InBlock.gif            return    m_Product;
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif        
#endregion

 当Reader只想取两三个字段(少于Entity的属性个数)时,如果不用try catch就会出错:

OutOfRangeException
再加上(reader["P_ID"]!=null), 也仍然出错。

 于是想到用Reflect 

得方法如下

None.gif Using System.Reflection 
ContractedBlock.gifExpandedBlockStart.gif
Product #region    Product
InBlock.gif        
internal static Downmoon.Product.Framework.Components.Product LoadSingleProduct(IDataReader reader)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Downmoon.Product.Framework.Components.Product m_Product 
= new Downmoon.Product.Framework.Components.Product();
InBlock.gif            Type type 
= m_Product.GetType();
InBlock.gif            
for (int i = 0; i < reader.FieldCount; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (!reader.IsDBNull(i))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
try dot.gif{ type.InvokeMember(reader.GetName(i), BindingFlags.Default | BindingFlags.SetProperty, null, m_Product, new object[] dot.gif{ reader.GetValue(i) }); }
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
catch (MissingMemberException exception) dot.gif{ System.Diagnostics.Debug.WriteLine(exception.Message); }
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return m_Product;
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif        
#endregion

 OK!

转载于:https://www.cnblogs.com/downmoon/archive/2008/08/27/1277433.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值