2014-02-07 141 views
0

我有一个程序,它以电子表格作为输入,并且如果有任何错误,则将错误转储回单独的电子表格,原始数据由用户输入并显示错误消息。获取通用对象的类名称

要保存错误的记录及其错误消息,我有一个Dictionary对象,其中可能包含两种类型的对象之一作为关键字。它将拥有一个DataRow对象或一个名称为ZipCodeTerritory的实体框架模型对象。

因为我有可能数据输入错误(即111/2/1980为日期),我需要创建我用来填充电子表格中的单独的错误类,我需要到DataRow/ZipCodeTerritory键转换成这特殊错误类。但是,在我这样做之前,我需要确定我正在使用哪种类型。我在下面使用的方法无法使用,或者使用DataRowZipCodeTerritory对象。任何人都知道更好的方法来确定一个通用对象的类型?

C#

//Definition of the Dictionary I'm using to hold the errored records/messages 
private static Dictionary<object, string> _errors = new Dictionary<object, string>(); 

//Snippet of code to show how I'm adding one of the ZipCodeTerritory objects 
//"record" object is of type ZipCodeTerritory 
if (string.IsNullOrEmpty(record.LastUpdateId)) 
{ 
    //Add to error list 
    _issues++; 
    _errors.Add(record, "Missing last update Id"); 
    continue; 
} 

//Section of code that tries to separate key/value and determine type 
foreach (KeyValuePair<object, string> item in errors) 
{ 
    //Use custom class in case of data errors 
    //resulting from bad input on spreadsheet 
    ZipCodeError zip; 

    //Determine type and separate key and value 
    Type type = item.Key.GetType(); 
    if (type.GetType().Name.Equals("DataRow")) 
    { 
     zip = new ZipCodeError((DataRow)item.Key); 
    } 
    else if (type.GetType().Name.Equals("ZipCodeTerritory")) 
    { 
     zip = new ZipCodeError((ZipCodeTerritory)item.Key); 
    } 
    else 
    { 
     //Code always falls through to this.... 
     zip = new ZipCodeError(); 
    } 

ZipCodeError

public class ZipCodeError 
{ 
    //Create generic object properties just in case of 
    //typos or any other unforseen input errors from the spreadsheet 
    public object ChannelCode { get; set; } 
    public object DrmTerrDesc { get; set; } 
    public object IndDistrnId { get; set; } 
    public object StateCode { get; set; } 
    public object ZipCode { get; set; } 
    public object EndDate { get; set; } 
    public object EffectiveDate { get; set; } 
    public object LastUpdateId { get; set; } 
    public object LastUpdateDate { get; set; } 
    public object ErrorCodes { get; set; } 
    public object Status { get; set; } 
    public object Id { get; set; } 

    public ZipCodeError(){} 

    public ZipCodeError(DataRow row) 
    { 
     this.ChannelCode = row[0]; 
     this.DrmTerrDesc = row[1]; 
     this.IndDistrnId = row[2]; 
     this.StateCode = row[3]; 
     this.ZipCode = row[4]; 
     this.EndDate = row[5]; 
     this.EffectiveDate = row[6]; 
     this.LastUpdateId = row[7]; 
     this.ErrorCodes = row[8]; 
     this.Status = row[9]; 
     this.Id = row[10]; 
    } 

    public ZipCodeError(ZipCodeTerritory master) 
    { 
     this.ChannelCode = master.ChannelCode; 
     this.DrmTerrDesc = master.DrmTerrDesc; 
     this.IndDistrnId = master.IndDistrnId; 
     this.StateCode = master.StateCode; 
     this.ZipCode = master.ZipCode; 
     this.EndDate = master.EndDate; 
     this.EffectiveDate = master.EffectiveDate; 
     this.LastUpdateDate = master.LastUpdateDate; 
     this.LastUpdateId = master.LastUpdateId; 
     this.ErrorCodes = master.ErrorCodes; 
     this.Status = master.Status; 
     this.Id = master.Id; 
    } 
}  
+0

什么叫GetType你'type.GetType()。Name'然后返回? – i3arnon

+2

为什么不使用''的字典?关于你在做什么还有其他一些问题,但那个问题真的很突出。不过,真的,如果你有两种类型的数据,我看不出有什么理由让它们保持在同一个集合中。一定会有更好的办法。 – Magus

回答

3

使用is关键字

if (item.Key is DataRow) 
{ 
    zip = new ZipCodeError((DataRow)item.Key); 
} 
else if (item.Key is ZipCodeTerritory) 
{ 
    zip = new ZipCodeError((ZipCodeTerritory)item.Key); 
} 
else 
{ 
    ... 
} 
2

你在你的imple有一个额外的GetType()心理状态。你总是让RuntimeType这是Type当你在Type

//Determine type and separate key and value 
Type type = item.Key.GetType(); 
if (type.Name.Equals("DataRow")) 
{ 
    zip = new ZipCodeError((DataRow)item.Key); 
} 
else if (type.Name.Equals("ZipCodeTerritory")) 
{ 
    zip = new ZipCodeError((ZipCodeTerritory)item.Key); 
} 
else 
{ 
    ... 
}