Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # All Class Object Properties as a String
- #### Simple
- ```CSharp
- Func<MyClass, string> MyFunc =
- (a) =>
- a
- .GetType()
- .GetProperties()
- .Select(p => p
- .GetValue(a));
- ```
- #### For CSV
- ```CSharp
- Func<MyClass, string> MyFunc =
- (a) => "\"" +
- string.Join("\",\"",
- a
- .GetType()
- .GetProperties()
- .Select(p => p
- .GetValue(a)))
- + "\"";
- ```
- ---
- ## Example
- ```CSharp
- public class User
- {
- public int Id { get; set; }
- public string FirstInitial { get; set; }
- public string LastName { get; set; }
- public bool IsStudent { get; set; }
- public string PhoneNumber { get; set; }
- public string Ethnicity { get; set; }
- ...
- }
- ```
- ```CSharp
- Func<User, string> GetAllProperties = (a) => "\"" + string.Join("\",\"", aGetType().GetProperties().Select(p => p.GetValue(a)))+ "\"";
- ...
- using(StreamWriter outFile = new StreamWriter(fp))
- {
- userList.ForEach(user => outFile.WriteLine(GetAllProperties(user)));
- }
- ```
- ### Output file
- ```txt
- "1159","J","English","false","",...,""
- ```
Add Comment
Please, Sign In to add comment