2010-08-10 29 views
6
public class Address 
{ 
    public string AddressLine1 { get; set; } 
    public string AddressLine2 { get; set; } 
    public string City { get; set; } 
    public string State { get; set; } 
    public string Zip { get; set; } 
} 
...... 
var emp1Address = new Address(); 
emp1Address.AddressLine1 = "Microsoft Corporation"; 
emp1Address.AddressLine2 = "One Microsoft Way"; 
emp1Address.City = "Redmond"; 
emp1Address.State = "WA"; 
emp1Address.Zip = "98052-6399"; 

Cân nhắc lớp ở trên và sau đó khởi tạo. Bây giờ tại một số điểm tôi muốn đăng nhập trạng thái của nó khi lỗi xảy ra. Tôi muốn nhận được chuỗi đăng nhập hơi giống như dưới đây.Trạng thái ghi nhật ký đối tượng. Nhận tất cả các giá trị thuộc tính của nó dưới dạng chuỗi

string toLog = Helper.GetLogFor(emp1Address); 

sting toLog phải trông giống như dưới đây.

AddressLine1 = "Microsoft Corporation"; 
AddressLine2 = "One Microsoft Way"; 
City = "Redmond"; 
State = "WA"; 
Zip = "98052-6399"; 

Và sau đó tôi sẽ ghi lại toLog chuỗi.

Tôi làm cách nào để truy cập tất cả các tên thuộc tính và giá trị thuộc tính của đối tượng trong phương thức Helper.GetLogFor()?

Giải pháp mà tôi thực hiện: -

/// <summary> 
/// Creates a string of all property value pair in the provided object instance 
/// </summary> 
/// <param name="objectToGetStateOf"></param> 
/// <exception cref="ArgumentException"></exception> 
/// <returns></returns> 
public static string GetLogFor(object objectToGetStateOf) 
{ 
    if (objectToGetStateOf == null) 
    { 
    const string PARAMETER_NAME = "objectToGetStateOf"; 
    throw new ArgumentException(string.Format("Parameter {0} cannot be null", PARAMETER_NAME), PARAMETER_NAME); 
    } 
    var builder = new StringBuilder(); 

    foreach (var property in objectToGetStateOf.GetType().GetProperties()) 
    { 
    object value = property.GetValue(objectToGetStateOf, null); 

     builder.Append(property.Name) 
     .Append(" = ") 
     .Append((value ?? "null")) 
     .AppendLine(); 
    } 
    return builder.ToString(); 
} 
+0

Đã có một số lý do để không sử dụng serialization? –

+0

Tôi muốn nó ở định dạng có thể đọc được để mọi khi tôi phải xem qua nhật ký, tôi có thể nhanh chóng thấy giá trị trong bất kỳ thuộc tính nào của đối tượng tại thời điểm xảy ra lỗi. – IsmailS

Trả lời

18
public static string GetLogFor(object target) 
{ 
    var properties = 
     from property in target.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance) 
     select new 
     { 
      Name = property.Name, 
      Value = property.GetValue(target, null) 
     }; 

    var builder = new StringBuilder(); 

    foreach(var property in properties) 
    { 
     builder 
      .Append(property.Name) 
      .Append(" = ") 
      .Append(property.Value) 
      .AppendLine(); 
    } 

    return builder.ToString(); 
} 
+0

'Đối số typ 'đối tượng' không thể gán cho kiểu tham số 'string'' lỗi biên dịch trên' builder.Append (property.Name) .Append ("=") .AppendLine (property.Value); 'statement – IsmailS

+0

Bạn có thể vui lòng giúp tôi hiểu' .GetProperties (BindingFlags.Public | BindingFlags.Instance) 'có nghĩa là gì? – IsmailS

+0

@Ismail: Tôi đã cập nhật mã của mình để xóa lỗi đó. Tôi không bao giờ nhận thấy rằng 'AppendLine' không có cùng một tập các quá tải như' Append'. –

5
static void Log(object @object) 
{ 
    foreach (var property in @object.GetType().GetProperties()) 
     Console.WriteLine(property.Name + ": " + property.GetValue(@object, null).ToString()); 
} 
+0

Cuộc gọi 'ToString' sẽ gây ra' NullReferenceException' cho giá trị thuộc tính null. –

+0

Tôi đã phải đặt một kiểm tra null trước khi truy cập vào giá trị của tài sản. – IsmailS

+0

Tôi thích người trả lời này vì nó không sử dụng LINQ – UpTheCreek

1

Bạn có thể truy cập vào tên tài sản sử dụng phản ánh như sau

Type t = emp1Address.GetType(); 


PropertyInfo [] pi = t.GetProperties(); 
    foreach (PropertyInfo p in pi) 
    { 

    //You can get the value (using GetValue() method) and name (p.Name) here. 



    } 
Các vấn đề liên quan