When creating a new item; is there any way to access all the field values that are set.
Since I'm using Entity.GetModifiedMembers() method to access the values of the fields that are changed when updating for logging purposes, the purpose is to have the equivalent result through an entity when creating, like a method Entity.GetSetMembers().
So in general, all I need is a key-value pair with "member name" and "value" items.
Example:
public class SomethingEntity
{
public int Id {get;set;}
public string Name {get;set;}
public DateTime Created {get;set;}
public DateTime Modified {get;set;}
}
public Dictionary<string, string> GetFieldsAndValuesOfCreatedItem(object entity)
{
//This is what I need, that can take all the objects from an entity and give
//the property-value pairs for the object instance
return RequieredMethod(entity);
}
public ExampleMethod()
{
var newObject = new SomethingEntity() { Name = "SomeName", Created = DateTime.Now };
Entity.insetOnSubmit(newObject);
Entity.SubmitChanges();
var resultList = GetFieldsAndValuesOfCreatedItem(newObject);
foreach (var propertyKeyValue in resultList)
{
var propertyString = "Property Name: " + propertyKeyValue.Key;
var valueString = "Value : " + propertyKeyValue.Value;
}
}
I've found out that, Reflection is the answer for that as far as I could find: so here is the method I've come up with:
public static Dictionary<string, string> GetFieldsAndValuesOfCreatedItem(object item)
{
var propertyInfoList = item.GetType().GetProperties(BindingFlags.DeclaredOnly |
BindingFlags.Public |
BindingFlags.Instance);
var list = new Dictionary<string, string>();
foreach (var propertyInfo in propertyInfoList)
{
var valueObject = propertyInfo.GetValue(item, null);
var value = valueObject != null ? valueObject.ToString() : string.Empty;
if (!string.IsNullOrEmpty(value))
{
list.Add(propertyInfo.Name, value);
}
}
return list;
}
Related
I need a method that will be able to loop through every static property in a static class, and combine these to a string with json syntax, where key name would equal to Property name, and key value would be the value of the property.
So the result would be a string with value:
{ "StaticPropertyName_string": "string_value_of_this_property", "StaticPropertyName_int": 34 }
I have a working method that successfully does the opposite - takes a json and maps data to static fields. But can't figure out how to do it in reverse
public static void MapProfileToJson(JToken source) //// source = plain json string
{
var destinationProperties = typeof(Fields)
.GetProperties(BindingFlags.Public | BindingFlags.Static);
foreach (JProperty prop in source)
{
var destinationProp = destinationProperties
.SingleOrDefault(p => p.Name.Equals(prop.Name, StringComparison.OrdinalIgnoreCase));
var value = ((JValue)prop.Value).Value;
if (typeof(Fields).GetProperty(prop.Name) != null)
destinationProp.SetValue(null, Convert.ChangeType(value, destinationProp.PropertyType));
else
Console.WriteLine("(!) property \"" + prop.Name + "\" is not found in class... skip");
}
}
Example static class:
public static class Fields
{
public static bool BoolField { get; set; }
public static string StringField { get; set; }
public static int IntField { get; set; }
public static double DoubleField { get; set; }
}
p.s.
Key value pairs saved in string, could be wrapped at the end like
ResultString = "{ " + resultString + " }";
C# .NET 4.7.2 (console app)
As others have said, a static class is not a good design here.
However, it is possible to map it back to JSON with some simple reflection:
public static JObject MapStaticClassToJson(Type staticClassToMap)
{
var result = new JObject();
var properties = staticClassToMap.GetProperties(BindingFlags.Public | BindingFlags.Static);
foreach (PropertyInfo prop in properties)
{
result.Add(new JProperty(prop.Name, prop.GetValue(null, null)));
}
return result;
}
I think it would be nice to have some simple code to assign json properties to a static class properties too. I created this code using #RichardDeeming answer
public static void MapStaticClassFromJson(string json, Type staticClassToMap)
{
var jsonObject = JObject.Parse(json);
var properties = staticClassToMap.GetProperties(BindingFlags.Public | BindingFlags.Static);
foreach (PropertyInfo prop in properties)
prop.SetValue(null, Convert.ChangeType(jsonObject[prop.Name], prop.PropertyType, CultureInfo.CurrentCulture), null);
}
test
MapStaticClassFromJson(json,typeof(Data));
Console.WriteLine(Data.StaticPropertyName_int);
Console.WriteLine(Data.StaticPropertyName_string);
result
34
string_value_of_this_property
There are a lot of answers for this using a single Type with interfaces and abstract classes which all work fine for one generic T value. What I am trying to achieve I have not seen and am wondering if anyone has an idea.
Scenario
public class Field<T>
{
public Expression<Func<T,object>> FieldName { get; set; }
}
public class FValue<T, F> : Field<T>
{
public F FieldValue { get; set; }
}
//Test
var fieldList = new List<Field<Person>>();
fieldList.Add(new FValue<Person, DateTime> { FieldName=x=>x.SomeDate, FieldValue=DateTime.Now });
fieldList.Add(new FValue<Person, string> { FieldName=x=>x.SomeData, FieldValue="test" });
Ideally i want to do the following:-
The list will contain the same type for T, the type for F will change to various types like date,string etc.
When iterating over the list i want both the FieldName and FieldValue
I can't start the list using new <List<FValue<Persion,string>>();
for the obvious reason that all F values will have to be string.
Also when obtaining the FieldName somehow the value should be casted to Expression<Func<T,F>>.
Any suggestions would be appreciated.
Whenever you want to use generics you need a specific reason to use it. See this answer about when to use Generics
What is cool about generics, why use them?
As you can see in the link, one of the main reason is to have type-safe properties. This also means that your class will be limited to the specific type. And taking this in consideration, the usages of your class will be limited.
Here is how I could use your class with limited usages that don't require (boxing/unboxing), but still requires casting
private static void UseFieldList<T>(List<Field<T>> fieldList)
{
foreach (var field in fieldList)
{
var propName = field.FieldNameText;
var textField = field as FValue<T, string>;
if (textField != null)
{
// Now can use string specific functions without (boxing/unboxing)
Console.WriteLine(propName + " " + textField.FieldValue );
continue;
}
var dateField = field as FValue<T, DateTime>;
if (dateField != null)
{
// Now can use date specific functions without (boxing/unboxing)
Console.WriteLine(propName + " " + dateField.FieldValue.ToShortDateString());
continue;
}
throw new NotSupportedException("The type of the field is not supported: " + field.GetType().Name);
}
}
To get the name out of a expression you can see the answer here
Retrieving Property name from lambda expression
And to use this I would change the way you are creating the objects to something similar to the usages of Tuple:
// Newer code storing name
fieldList.Add(FValue<Person>.Create(x => x.DateOfBirth, DateTime.Now ));
fieldList.Add(FValue<Person>.Create(x => x.Name, "test"));
// Old code storing expression instead of name
fieldList.Add(new FValue<Person, DateTime> { FieldName = x => x.DateOfBirth, FieldValue = DateTime.Now });
fieldList.Add(new FValue<Person, string> { FieldName = x => x.Name, FieldValue = "test" });
// Not supported Type Int
fieldList.Add(new FValue<Person, int> {FieldName = x => x.Name, FieldValue = 12});
And here is the factory class
public class FValue<T>
{
public static Field<T> Create<F>(Expression<Func<T, F>> fieldNameExpression, F value)
{
return new FValue<T, F>
{
FieldNameText = GetPropertyInfo(fieldNameExpression).Name,
FieldValue = value
};
}
}
Results of the console:
DateOfBirth 1/19/2017
Name test
x => Convert(x.DateOfBirth) 1/19/2017
x => x.Name test
The type of the field is not supported: FValue`2
I'm not sure I see a way around this one without using Reflection. Using these classes:
public class Field<T>
{
public Expression<Func<T, object>> FieldName { get; set; }
}
public class FValue<T, F> : Field<T>
{
public F FieldValue { get; set; }
}
You can iterate over them like so:
var list = new List<Field<string>>();
list.Add(new FValue<string, int>() { FieldName = null, FieldValue = 5 });
foreach (var x in list)
{
Type[] types = x.GetType().GetGenericArguments();
// Dirty check to confirm this is an FValue not a Field
if (types.Length == 2)
{
var fieldName = x.FieldName;
object fieldValue = x.GetType().GetProperty("FieldValue").GetValue(x);
// fieldValue will be "5"
}
}
I want to create a query of type Expression that gets some columns of an entity from Entity Framework.
Assume we have two classes like this:
public class Parent
{
public int Id { get; set; }
public string Name { get; set; }
public Child MyChild { get; set; }
}
public class Child
{
public int Id { get; set; }
public string Name { get; set; }
}
And we have an IQueryable list of Parent:
var q = new List<Parent>()
{
new Parent {Id = 1, Name = "a", Number = 1, MyChild=new Child{Id=11,Name="Child_a",Number=2}},
new Parent {Id = 2, Name = "b", Number = 1, MyChild=new Child{Id=22,Name="Child_b",Number=2}},
new Parent {Id = 3, Name = "c", Number = 1, MyChild=new Child{Id=33,Name="Child_c",Number=2}},
}.AsQueryable();
I want to get a list of those properties of q that user determines them. For example user determines that he needs Parent.Name and Parent.MyChils.Name. So I should give the user a list of anonymous type like this:
{"a","Child_a"}
{"b","Child_b"}
{"c","Child_c"}
If the Parent entity doesn't contain any foreign key property (in this example MyChild property) it is so easy to create an expression property that takes some properties of Parent dynamically. I have a code that gets some properties of Person without MyChild property of it:
var columns = new List<string> { "Id", "Name" };
var xParam = Expression.Parameter(typeof(Parent), "x");
var sourceProperties = columns.ToDictionary(name => name,
name => q.ElementType.GetProperty(name));
var dynamicType = LinqRuntimeTypeBuilder.GetDynamicType(sourceProperties.Values);
var bindings =
dynamicType.GetFields()
.Select(p => Expression.Bind(p, Expression.Property(xParam, sourceProperties[p.Name])))
.OfType<MemberBinding>();
var newExpr = Expression.New(dynamicType.GetConstructor(Type.EmptyTypes));
Expression selector = Expression.Lambda(Expression.MemberInit(
Expression.New(dynamicType.GetConstructor(Type.EmptyTypes)), bindings), xParam);
var body = Expression.MemberInit(newExpr, bindings);
var lambda = Expression.Lambda<Func<Parent, dynamic>>(body, xParam);
var t = q.Select(lambda);
(2 used methods are here:)
public static Type GetDynamicType2(Dictionary<string, Type> fields)
{
if (null == fields)
throw new ArgumentNullException("fields");
if (0 == fields.Count)
throw new ArgumentOutOfRangeException("fields", "fields must have at least 1 field definition");
try
{
Monitor.Enter(builtTypes);
string className = "MyDynamicType";
if (builtTypes.ContainsKey(className))
return builtTypes[className];
TypeBuilder typeBuilder = moduleBuilder.DefineType(className,
TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.Serializable);
foreach (var field in fields)
typeBuilder.DefineField(field.Key, field.Value, FieldAttributes.Public);
builtTypes[className] = typeBuilder.CreateType();
return builtTypes[className];
}
catch (Exception ex)
{
}
finally
{
Monitor.Exit(builtTypes);
}
return null;
}
public static Type GetDynamicType(IEnumerable<PropertyInfo> fields)
{
return GetDynamicType(fields.ToDictionary(f => f.Name, f => f.PropertyType));
}
But still I can't get internal properties of MyChild property of Parent.
How to do that?
Since nobody answered my question I tried many different ways to solve it, upshot problem solved utilizing recursive creation of Expression.
For every property of type Class (like MyChild in the question) we most create an Expression. Creation of Expressions most be recursive like this:
private Expression BuildExpression(Expression parentExpression,
Type parentPropertyType, string pathOfChildProperty)
{
string remainPathOfChild;
var childPropertyName =
GetFirstPropertyNameFromPathAndRemainPath(pathOfChildProperty, out remainPathOfChild);
if (string.IsNullOrEmpty(childPropertyName)) return parentExpression;
var childPropInfo = parentPropertyType.GetProperty(childPropertyName);
var childExpression = Expression.Property(parentExpression, childPropInfo);
return !string.IsNullOrEmpty(remainPathOfChild)
? BuildExpressionForInternalProperty(childExpression, childPropInfo.PropertyType, remainPathOfChild)
: childExpression;
}
private string GetFirstPropertyNameFromPathAndRemainPath(string path, out string remainPath)
{
if (string.IsNullOrEmpty(path))
{
remainPath = null;
return null;
}
var indexOfDot = path.IndexOf('.');
if (indexOfDot < 0)
{
remainPath = null;
return path;
}
remainPath = path.Substring(indexOfDot + 1);
return path.Substring(0, indexOfDot);
}
}
Recursive call will stop when remain path of internal property be empty.
In highest level the calling of this method is like this:
var inputParameter = Expression.Parameter(typeof(GrandParent), "x");
var expChildProperty =
BuildExpression(inputParameter,typeof(Parent),"Parent.MyChils.Name");
Finally result expression for above problem is (($x.Parent).MyChils).Name in debug view.
i am trying to "upcast" a Entity. The Entity B have just a few more propeties.
Entities:
public class A
{
public String Name { get; set; }
}
public class B : A
{
public String ForeName { get; set; }
}
I am trying to conver the value of A.Name into B.Name programmatically.
I´ve written a little function for that case:
public static T Upcast<T>(Type typeOf, Object obj) where T : new()
{
var target = new T();
var props = obj.GetType().GetProperties();
var targetProps = target.GetType().GetProperties();
foreach (var prop in props)
{
foreach (var tp in targetProps)
{
if (prop.Name.Equals(tp.Name))
{
var val = prop.GetValue(props, null);
tp.SetValue(null, val, null);
}
}
}
return target;
}
In this case: my target is class B and my obj is class A
But it fires a Exception in the second loop:
var val = prop.GetValue(props, null);
The Exception "Object does not match target type".
i am calling the function on a normal way:
static void Main(string[] args)
{
var a = new A {Name = "Smith"};
var resp = Upcast<B>(a.GetType(), a);
Console.ReadLine();
}
Replace prop.GetValue(props, null); with prop.GetValue(obj, null);
It gives me "Smith".
You must call prop.GetValue(...); with the target object as argument, not with the property collection:
prop.GetValue(obj, null);
Starting with .NET 4.5 you can write
var val = prop.GetValue(obj);
You are trying the read the value out of an instance of PropertyInfo class. But what you want is to read the value out of your instance of A.
Another hint, for cleaner code:
Don't pass the type of A as argument. If you pass an instance of A, your method can make obj.GetType().
But: why are you doing that???
Im looking for a way to create CSV from all class instances.
What i want is that i could export ANY class (all of its instances) to CSV.
Can some1 direct me to possible solution for this (in case already anwsered).
thanx !
Have a look at LINQ to CSV. Although it's a little on the heavy side, which is why I wrote the following code to perform just the small subset of functionality that I needed. It handles both properties and fields, like you asked for, although not much else. One thing it does do is properly escape the output in case it contains commas, quotes, or newline characters.
public static class CsvSerializer {
/// <summary>
/// Serialize objects to Comma Separated Value (CSV) format [1].
///
/// Rather than try to serialize arbitrarily complex types with this
/// function, it is better, given type A, to specify a new type, A'.
/// Have the constructor of A' accept an object of type A, then assign
/// the relevant values to appropriately named fields or properties on
/// the A' object.
///
/// [1] http://tools.ietf.org/html/rfc4180
/// </summary>
public static void Serialize<T>(TextWriter output, IEnumerable<T> objects) {
var fields =
from mi in typeof (T).GetMembers(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static)
where new [] { MemberTypes.Field, MemberTypes.Property }.Contains(mi.MemberType)
let orderAttr = (ColumnOrderAttribute) Attribute.GetCustomAttribute(mi, typeof (ColumnOrderAttribute))
orderby orderAttr == null ? int.MaxValue : orderAttr.Order, mi.Name
select mi;
output.WriteLine(QuoteRecord(fields.Select(f => f.Name)));
foreach (var record in objects) {
output.WriteLine(QuoteRecord(FormatObject(fields, record)));
}
}
static IEnumerable<string> FormatObject<T>(IEnumerable<MemberInfo> fields, T record) {
foreach (var field in fields) {
if (field is FieldInfo) {
var fi = (FieldInfo) field;
yield return Convert.ToString(fi.GetValue(record));
} else if (field is PropertyInfo) {
var pi = (PropertyInfo) field;
yield return Convert.ToString(pi.GetValue(record, null));
} else {
throw new Exception("Unhandled case.");
}
}
}
const string CsvSeparator = ",";
static string QuoteRecord(IEnumerable<string> record) {
return String.Join(CsvSeparator, record.Select(field => QuoteField(field)).ToArray());
}
static string QuoteField(string field) {
if (String.IsNullOrEmpty(field)) {
return "\"\"";
} else if (field.Contains(CsvSeparator) || field.Contains("\"") || field.Contains("\r") || field.Contains("\n")) {
return String.Format("\"{0}\"", field.Replace("\"", "\"\""));
} else {
return field;
}
}
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public class ColumnOrderAttribute : Attribute {
public int Order { get; private set; }
public ColumnOrderAttribute(int order) { Order = order; }
}
}
Actually, something similar has been addressed here:
Best practices for serializing objects to a custom string format for use in an output file
Is this useful to you?
There is a sample that uses reflection to pull out the field names and values and append them to a string.
You can use reflection to traverse all the class properties/fields and write them to CSV.
A better approach would be to define a custom attribute and decorate the members you want to export and only export those attributes.
I am separating my answer into two sections:
The first one is how to export some generic item list into csv, with encoding, headers - (it will build csv data only for specified headers, and will ignore unneeded properties).
public string ExportCsv<T>(IEnumerable<T> items, Dictionary<string, string> headers)
{
string result;
using (TextWriter textWriter = new StreamWriter(myStream, myEncoding))
{
result = this.WriteDataAsCsvWriter<T>(items, textWriter, headers);
}
return result;
}
private string WriteDataAsCsvWriter<T>(IEnumerable<T> items, TextWriter textWriter, Dictionary<string, string> headers)
{
//Add null validation
////print the columns headers
StringBuilder sb = new StringBuilder();
//Headers
foreach (KeyValuePair<string, string> kvp in headers)
{
sb.Append(ToCsv(kvp.Value));
sb.Append(",");
}
sb.Remove(sb.Length - 1, 1);//the last ','
sb.Append(Environment.NewLine);
//the values
foreach (var item in items)
{
try
{
Dictionary<string, string> values = GetPropertiesValues(item, headers);
foreach (var value in values)
{
sb.Append(ToCsv(value.Value));
sb.Append(",");
}
sb.Remove(sb.Length - 1, 1);//the last ','
sb.Append(Environment.NewLine);
}
catch (Exception e1)
{
//do something
}
}
textWriter.Write(sb.ToString());
return sb.ToString();
}
//Help function that encode text to csv:
public static string ToCsv(string input)
{
if (input != null)
{
input = input.Replace("\r\n", string.Empty)
.Replace("\r", string.Empty)
.Replace("\n", string.Empty);
if (input.Contains("\""))
{
input = input.Replace("\"", "\"\"");
}
input = "\"" + input + "\"";
}
return input;
}
This is the most important function, Its extracting the properties values out of (almost) any generic class.
private Dictionary<string, string> GetPropertiesValues(object item, Dictionary<string, string> headers)
{
Dictionary<string, string> values = new Dictionary<string, string>();
if (item == null)
{
return values;
}
//We need to make sure each value is coordinated with the headers, empty string
foreach (var key in headers.Keys)
{
values[key] = String.Empty;
}
Type t = item.GetType();
PropertyInfo[] propertiesInfo = t.GetProperties();
foreach (PropertyInfo propertiyInfo in propertiesInfo)
{
//it not complex: string, int, bool, Enum
if ((propertiyInfo.PropertyType.Module.ScopeName == "CommonLanguageRuntimeLibrary") || propertiyInfo.PropertyType.IsEnum)
{
if (headers.ContainsKey(propertiyInfo.Name))
{
var value = propertiyInfo.GetValue(item, null);
if (value != null)
{
values[propertiyInfo.Name] = value.ToString();
}
}
}
else//It's complex property
{
if (propertiyInfo.GetIndexParameters().Length == 0)
{
Dictionary<string, string> lst = GetPropertiesValues(propertiyInfo.GetValue(item, null), headers);
foreach (var value in lst)
{
if (!string.IsNullOrEmpty(value.Value))
{
values[value.Key] = value.Value;
}
}
}
}
}
return values;
}
Example for GetPropertiesValues:
public MyClass
{
public string Name {get; set;}
public MyEnum Type {get; set;}
public MyClass2 Child {get; set;}
}
public MyClass2
{
public int Age {get; set;}
public DateTime MyDate {get; set;}
}
MyClass myClass = new MyClass()
{
Name = "Bruce",
Type = MyEnum.Sometype,
Child = new MyClass2()
{
Age = 18,
MyDate = DateTime.Now()
}
};
Dictionary<string, string> headers = new Dictionary<string, string>();
headers.Add("Name", "CustomCaption_Name");
headers.Add("Type", "CustomCaption_Type");
headers.Add("Age", "CustomCaption_Age");
GetPropertiesValues(myClass, headers)); // OUTPUT: {{"Name","Bruce"},{"Type","Sometype"},{"Age","18"}}
My answer is based on Michael Kropat's answer from above.
I added two functions to his answer because it didn't want to write straight to file as I still had some further processing to do. Instead I wanted the header information separate to the values so I could put everything back together later.
public static string ToCsvString<T>(T obj)
{
var fields =
from mi in typeof(T).GetMembers(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static)
where new[] { MemberTypes.Field, MemberTypes.Property }.Contains(mi.MemberType)
let orderAttr = (ColumnOrderAttribute)Attribute.GetCustomAttribute(mi, typeof(ColumnOrderAttribute))
select mi;
return QuoteRecord(FormatObject(fields, obj));
}
public static string GetCsvHeader<T>(T obj)
{
var fields =
from mi in typeof(T).GetMembers(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static)
where new[] { MemberTypes.Field, MemberTypes.Property }.Contains(mi.MemberType)
let orderAttr = (ColumnOrderAttribute)Attribute.GetCustomAttribute(mi, typeof(ColumnOrderAttribute))
select mi;
return QuoteRecord(fields.Select(f => f.Name));
}