Setting Order in the properties of a class - c#

I have a class with 10 properties and I am using reflection to assign values to these properties. My Property names are like,
private double? _col1;
public double? Col1
{
get
{
return _col1;
}
set
{
_col1 = value;
OnPropertyChanged("Col1");
}
}
private double? _col2;
public double? Col2
{
get
{
return _col2;
}
set
{
_col2 = value;
OnPropertyChanged("Col2");
}
}
And I am using them like,
MyClass md = new MyClass();
PropertyInfo[] properties = typeof(MyClass).GetProperties();
{
foreach (PropertyInfo property in properties)
{
double? d1 = from some method();
if (property.PropertyType == typeof(Double?))
{
if (property.GetValue(md) == null)
{
property.SetValue(md, d1);
}
}
}
}
Here I want to use an orderby property name of the class. How ??
PropertyInfo[] properties = typeof(MyClass).GetProperties();

You need to sort properties alphabetically based on their name.
Add the following line before the foreach() loop
Array.Sort(properties, (x,y)=> x.Name.CompareTo(y.Name)

I think this is an XY problem.
I think what you actually need could be something like this:
public class MyClass
{
public ObservableCollection<double?> Cols { get; }
public MyClass(int initialSize)
{
if(initialSize < 0)
{
throw new ArgumentOutOfRangeException(nameof(initialSize));
}
Cols = new(Enumerable.Repeat((double?)null, initialSize));
}
}
Then, ditching the reflection, you can just use a for loop on the observable collection. The observable collection is great cause it already raises events informing about changes.
MyClass md = new MyClass(2);
for (int i = 0; i < md.Cols.Count; i++)
{
double? d1 = from some method();
if (md.Cols[i] is null)
{
md.Cols[i] = d1;
}
}
Then, if someone needs to listen to changes made to Cols, they subscribe to the Cols.CollectionChanged event.

Order of properties in reflection is the same as in their declaration in program code. Property declared first is first. Property declared second is second. I have never tested myself, but in case of partial classes, when a class is split into several files, the situation might be tricky.
how we can ensure that it takes first Col1, then secondly Col2, etc ??
To ensure, You should use names of the properties. Example
if (property.Name == nameof(MyClass.Col1))
{
property.SetValue(instanceOfClassName, value);
}
Alternative for if is switch statement
switch (property.Name)
{
case nameof(MyClass.Col1):
break;
case nameof(MyClass.Col2):
break;
}
If you want to order the properties by name then simply use OrderBy LINQ query
var orderedByNameProperties = properties.OrderBy(property => property.Name);

Related

How to combine many properties into one List<string> property in C#

I am a little bit confused about how to read data from Excel. I am trying to import Excel for updating product list, I create an Excel model; I added all basic properties like name, price, quantity, etc. into this model. I will read all Excel and map into this model. That's ok, then I will give this model to EF Core 5 to save to SQL Server.
public class ExcelModel
{
public string Name { get; set }
public int Price { get; set }
public int Quantity { get; set }
}
I have a problem with product options. According to my DB schema, I have one table of products, one for options, one for option values, one for productOptionRelation.
Can you suggest another solution way or just solve on my way?
My colleges did this created field corresponding to values. like option1 and optionValue1, option2 and optionValue2 many of them, because each product could have many options. Model look like that, 20 option and 20 value was declared here and they manually map all these
For a temporary solution, I limited this option up to 5 and I created an list. and encapsulate all of them into list
public class ExcelOptionViewModel
{
public string Option { get; set; }
public string Value { get; set; }
}
This is my temp model, I encapsulated like that.
public IList<ExcelOptionViewModel> OptionModels { get; set; } = new List<ExcelOptionViewModel>();
public string Option1
{
get { return OptionModels[0].Option; }
set
{
this.OptionModels.Insert(0, new ExcelOptionViewModel { Option = value });
}
}
public string Option1Value
{
get { return OptionModels[0].Value; }
set { this.OptionModels[0].Value = value; }
}
This would be unlimited, You should enter how much you want
I have 2 solutions still I am researching one is, creating a method inside the excelviewmodel, this method will add all options and values into a list or I will use reflection, I am looking something like underlying type I will all option and values this underlying base type or something, when property loop came here, checking the type and assign all option1,option2,option3 or name like that properties to List<string> options, and same for the option values. I will use reading like option[0] and optionvalue[0]
Excel column names must be different because I read excel and turn it into datatable. Datatable column names must be different, it's not valid for reading into datatable
I used basically excel to data table function I can't remember but probably I found it in StackOverflow. Also, I added a feature there If some cell is null it will miss.
public List<T> ConvertDataTableToList<T>(DataTable dt)
{
//datatable clomun names
var columnNames = dt.Columns.Cast<DataColumn>().Select(c => c.ColumnName.ToLower()).ToList();
//selection properties equals to columnnames because I dont want loop for all props
var properties = typeof(T).GetProperties().Where(prp => columnNames.Any(t => t.ToLower() == prp.Name.ToLower()));
return dt.AsEnumerable().Select(row =>
{
var objT = Activator.CreateInstance<T>();
foreach (var pro in properties)
{
try
{
if (row[pro.Name] != DBNull.Value)
pro.SetValue(objT, row[pro.Name], null);
else
pro.SetValue(objT, null, null);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
return objT;
}).ToList();
}
I am looking something here when option1 or option2 comes here it would put this into a list
Also in my dt to model converter I dont want to use If but if some data value is null It throws an error which cant convert from dbnull value. If you have a suggest for it I would like release if condition :)
When All done I will map this excelviewmodel to product model something like this
foreach (var prop in SideParams.columns)
{
var source = row.GetType().GetProperty(prop);
var destination = product.GetType().GetProperty(prop);
if (destination != null && source.GetValue(row) != null)
{
Type t = Nullable.GetUnderlyingType(destination.PropertyType) ?? destination.PropertyType;
object safeValue = Convert.ChangeType(source.GetValue(row), t);
destination.SetValue(product, safeValue);
}
}
I saw something here
https://learn.microsoft.com/en-us/dotnet/api/system.reflection.bindingflags?view=net-6.0
it about binding flangs when reflecting model. "Specifies flags that control binding and the way in which the search for members and types is conducted by reflection." If there is way I can redirect option(1-2-3-4-5-6...) to list options
thanks for the help I solved my problem. If you need something like that, my solution is;
As you know OptionModels is what I created before, AddOptipns function is a new one I use for add data to list,
The function work with the ref, otherwise it must be static, if I turn it static, option models also must be static, so I can't access the list.
public IList<ExcelOptionViewModel> OptionModels { get; set; } = new List<ExcelOptionViewModel>();
public void AddOptions(ref String option, ref String value)
{
OptionModels.Add(new ExcelOptionViewModel { Option = option.Trim(), Value = value.Trim() });
}
And also add some new parts to convert model function,
calling that AddOptions method with reflection, I got an example from here
https://learn.microsoft.com/en-us/dotnet/api/system.reflection.bindingflags?view=net-6.0
I was inspired by the swap example there.
public List<T> ConvertDataTableToList<T>(DataTable dt)
{
var columnNames = dt.Columns.Cast<DataColumn>().Select(c => c.ColumnName.ToLower()).ToList();
//selection properties equals to columnnames because I dont want loop for all props
var type = typeof(T);
var properties = type.GetProperties().Where(prp => columnNames.Any(t => t.ToLower() == prp.Name.ToLower())).ToList();
var productOptions = columnNames.Where(x => x.Contains("option")).ToList() ?? new List<string>();
return dt.AsEnumerable().Select(row =>
{
var objT = Activator.CreateInstance<T>();
foreach (var pro in properties)
{
try
{
if (row[pro.Name] != DBNull.Value)
pro.SetValue(objT, row[pro.Name], null);
else
pro.SetValue(objT, null, null);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
for (var i = 0; i < productOptions.Count(); i += 2)
{
object[] argValues = new object[] { row[productOptions[i]].ToString(), row[productOptions[i + 1]].ToString() };
String[] argNames = new String[] { "option", "value" } ;
var method = type.GetMethod("AddOptions");
method.Invoke(objT, argValues);
}
return objT;
}).ToList();
}
here is the added data :)

CategoryAttribute and PropertyInfo

I have a class that includes several hundred Properties. Each of the properties was declared with a [CategoryAttribute("My Category Name")] attribute clause so that it will display nicely in a PropertyGrid. I would like to make use of this same CategoryAttribute attribute to set the values of all the properties in the class that were labeled with specific categoryAttribute catagories. The code below compiles and runs but it doesn't accomplish the task because att_coll doesn't contain the CategoryAttribute attribute that I expected that it would. Does anyone know how to do this? Thanks so much.
class my_class
{
[CategoryAttribute("Category One")]
public int property_1
{
get { return _property_1; }
set { _property_1 = value; }
}
[CategoryAttribute("Category One")]
public int property_2
{
get { return _property_2; }
set { _property_2 = value; }
}
}
void ClearCatagory(string category_name)
{
CategoryAttribute target_attribute = new CategoryAttribute(category_name);
Type my_class_type = my_class.GetType();
PropertyInfo[] prop_info_array = my_class_type.GetProperties();
foreach (PropertyInfo prop_info in prop_info_array)
{
AttributeCollection att_coll = TypeDescriptor.GetAttributes(prop_info);
CategoryAttribute ca = (CategoryAttribute) att_col[typeof(CategoryAttribute)];
if (ca.Equals(target_attribute))
{
prop_info.SetValue(my_class, 0, null);
}
}
}
Use the MemberInfo.GetCustomAttributes instance method instead of TypeDescriptor.GetAttriburtes.
The call would be object[] attributes = prop_info.GetCustomAttributes(typeof(CategoryAttriute), false).
Or you could use TypeDescriptor.GetProperties instead of Type.GetProperties You shouldn't switch between using reflection and TypeDescriptor.
Also, the documentation for Category.Equals isn't exactly clear, but likely it implements reference equality (which is the default for C# unless a class specifically overrides it). Which means that Equals will only return true if the instances being compared are exactly the same regardless of the value of Category. If that is the case, then ca.Equals(target_attribute) will always be false because the references are different objects.
Try instead comparing the string value stored in the Category value. Strings implement value equality so that String.Equals compares the values stored in the stings.
So replace
if (ca.Equals(target_attribute))
with
if (ca.Cateogry.Equals(category_name))
Thanks so much to shf301 for solving this. Here is a working version of the code:
class my_class
{
[CategoryAttribute("Category One")]
public int property_1
{
get { return _property_1; }
set { _property_1 = value; }
}
}
void ClearCatagory(string category_name)
{
Type my_class_type = my_class.GetType();
PropertyInfo[] prop_info_array = my_class_type.GetProperties();
foreach (PropertyInfo prop_info in prop_info_array)
{
CategoryAttribute[] attributes = (CategoryAttribute[]) prop_info.GetCustomAttributes(typeof(CategoryAttribute), false);
foreach(CategoryAttribute ca in attributes)
{
if (ca.Category == category_name)
{
prop_info.SetValue(my_class, 0, null);
}
}
}
}
public class cls
{
[Category("Default")]
[DisplayName("Street")]
public string Street { get; set; }
}
foreach (PropertyInfo propinf in cls.GetProperties())
{
var category = prop.CustomAttributes.Where(x => x.AttributeType ==typeof(CategoryAttribute)).First();
sCategory = category.ConstructorArguments[0].Value.ToString();
}
This is how we could get value of CustomAttribute

Code snippet: create an "alias" for something else

I was looking for a similar way to create an alias for something else like its possible in C using preprocessor (this question is a bit similar, couldn't find anything useful there).
This is the problem: I've got a method that receives an array, but each position of the array has a specific meaning, like they where different parameters with specific names. What I want to do is to make my code easier to read (and write) by using those specific names, but, on the other hand, I don't want to create another method call (like in example 1) nor assign the array positions to new variables (example 2), because the performance is critical.
Example 1:
void OriginalMethodSignature(Type[] values)
{
SimplifiedMethod(values[0], values[1], ... values[n]);
}
void SimplifiedMethod(Type specificName1, Type specificName2, ... Type specificNameN)
{
// simple implementation using specific names instead of values[n]
}
Example 2:
void OriginalMethodSignature(Type[] values)
{
Type specificName1 = values[0];
Type specificName2 = values[1];
...
Type specificNameN = values[n];
// simple implementation using specific names instead of values[n]
}
I cannot change the method signature because its used in a dellegate, the Type is fixed.
The next example is a bit better, but still not optimum:
void OriginalMethodSignature(Type[] values)
{
// implementation using values[specificName1] ... values [specificNameN]
}
const int specificName1 = 0;
const int specificName2 = 1;
...
const int specificNameN = n-1;
Is there any way to create an snippet for this purpose? If yes, how would it be?
There isn't any built in way to do what you wan't, because you shouldn't really be doing it at all. You should be using an object with properties instead of an array.
Anyway, you can make an object that encapsulates the array, so that the properties use the array as storage:
public class NamedObject {
private Type[] _values;
public NamedObject(Type[] values) {
_values = values;
}
public SpecificName1 { get { return _values[0]; } set { _values[0] = value; } }
public SpecificName2 { get { return _values[1]; } set { _values[1] = value; } }
public SpecificName3 { get { return _values[2]; } set { _values[2] = value; } }
public SpecificName4 { get { return _values[3]; } set { _values[3] = value; } }
public SpecificName5 { get { return _values[4]; } set { _values[4] = value; } }
public SpecificName6 { get { return _values[5]; } set { _values[5] = value; } }
}
Now you can use the object to access the array:
void OriginalMethodSignature(Type[] values) {
NamedObject obj = new NamedObject(values);
// get a value
Type x = obj.SpecificName4;
// set a value
obj.SpecificName2 = x;
}
Create a dedicated class or struct, and parse the array into it.
public class MyClassOfStuff
{
Type SpecificName1 {get;set;}
Type SpecificName2 {get;set;}
public static MyClassOfStuff Parse(Type[] value)
{
Type specificName1 = values[0];
Type specificName2 = values[1];
...
Type specificNameN = values[n];
}
}
void OriginalMethodSignature(Type[] values)
{
var mystuff = MyClassOfStuff.Parse(values);
}

How to iterate on a T object to find a property?

I have this code that I am currently adapting to use nested entity (entity framework). I have an entity which contains 2 property which are 2 children entities.
The first step was to read the metadata on both classes, starting from the first classes, building up a list of properties. This is completed. Now I need to iterate over my object to find the good property to do the DataBinding.
This is what I currently have :
variables example :
datasource = namespace.A
propriete = {System.Nullable`1[System.Int32] count}
propriete.DeclaringType {Name = "prop2" FullName = "Namespace.Metadata.prop2"}
code :
if (this.datasource != null)
{
var y = (T)this.datasource;
var propList = typeof(T).GetProperties();
if (propList.ToList().Contains(propriete))
{
TextBox.Text = DataBinder.Eval(y, propriete.Name).ToString();
}
else
{
TextBox.Text = ":( need child-support!";
}
}
My main problem is that my object type is unknown till runtime (Type T) so I have no idea on how to find my field.
Quick model :
Class A {
public B prop1;
public C prop2;
}
Class B {
int count;
string name;
}
Class C {
int count;
string name;
}
A.prop1.count = 1;
A.prop1.name = "a";
A.prop2.count = 2;
A.prop2.name = "b";
Right now, my property name are all unique (more specific than count/name), but I expect them to be the same (count/name) at some point.
propriete will probably have to "filter" with DeclaringType/ReflectedType for non-unique name.
A brute-force solution considering unique name, although not elegant, might be accepted.
Extra problem : propriete use another partial class contains in the metadata namespace while datasource use the main class.
(... And if you are curious as to what this system does : It builds a html table (with .net controls) based on an entity based on this metadata.entity dataAttribute.)
You can get to the type information by calling GetType() on an instance. So in your case it would be:
var propList = this.datasource.GetType().GetProperties()
Here's what I ended up with :
if (this.datasource != null)
{
var y = (T)this.datasource;
var propList = typeof(T).GetProperties();
//try to assign the prop but it might be on a child-prop.
try
{
retour = DataBinder.Eval(y, propriete.Name).ToString();
}
catch
{
foreach (PropertyInfo prop in propList)
{
if ((prop.PropertyType).FullName.Contains("Env."))
{
var childPropList = prop.PropertyType.GetProperties();
foreach (PropertyInfo childProp in childPropList)
{
if (((System.Reflection.MemberInfo)(childProp)).Name == propriete.Name)
{
var x = DataBinder.GetPropertyValue(y, prop.Name);
retour = DataBinder.Eval(x, propriete.Name).ToString();
}
}
}
}
}
}
For now it works, but it might not be versatile enough in a near future.

How to get current property name via reflection?

I would like to get property name when I'm in it via reflection. Is it possible?
I have code like this:
public CarType Car
{
get { return (Wheel) this["Wheel"];}
set { this["Wheel"] = value; }
}
And because I need more properties like this I would like to do something like this:
public CarType Car
{
get { return (Wheel) this[GetThisPropertyName()];}
set { this[GetThisPropertyName()] = value; }
}
Since properties are really just methods you can do this and clean up the get_ returned:
class Program
{
static void Main(string[] args)
{
Program p = new Program();
var x = p.Something;
Console.ReadLine();
}
public string Something
{
get
{
return MethodBase.GetCurrentMethod().Name;
}
}
}
If you profile the performance you should find MethodBase.GetCurrentMethod() is miles faster than StackFrame. In .NET 1.1 you will also have issues with StackFrame in release mode (from memory I think I found it was 3x faster).
That said I'm sure the performance issue won't cause too much of a problem- though an interesting discussion on StackFrame slowness can be found here.
I guess another option if you were concerned about performance would be to create a Visual Studio Intellisense Code Snippet that creates the property for you and also creates a string that corresponds to the property name.
Slightly confusing example you presented, unless I just don't get it.
From C# 6.0 you can use the nameof operator.
public CarType MyProperty
{
get { return (CarType)this[nameof(MyProperty)]};
set { this[nameof(MyProperty)] = value]};
}
If you have a method that handles your getter/setter anyway, you can use the C# 4.5 CallerMemberName attribute, in this case you don't even need to repeat the name.
public CarType MyProperty
{
get { return Get<CarType>(); }
set { Set(value); }
}
public T Get<T>([CallerMemberName]string name = null)
{
return (T)this[name];
}
public void Set<T>(T value, [CallerMemberName]string name = null)
{
this[name] = value;
}
I'd like to know more about the context in which you need it since it seems to me that you should already know what property you are working with in the property accessor. If you must, though, you could probably use MethodBase.GetCurrentMethod().Name and remove anything after get_/set_.
Update:
Based on your changes, I would say that you should use inheritance rather than reflection. I don't know what data is in your dictionary, but it seems to me that you really want to have different Car classes, say Sedan, Roadster, Buggy, StationWagon, not keep the type in a local variable. Then you would have implementations of methods that do the proper thing for that type of Car. Instead of finding out what kind of car you have, then doing something, you then simply call the appropriate method and the Car object does the right thing based on what type it is.
public interface ICar
{
void Drive( decimal velocity, Orientation orientation );
void Shift( int gear );
...
}
public abstract class Car : ICar
{
public virtual void Drive( decimal velocity, Orientation orientation )
{
...some default implementation...
}
public abstract void Shift( int gear );
...
}
public class AutomaticTransmission : Car
{
public override void Shift( int gear )
{
...some specific implementation...
}
}
public class ManualTransmission : Car
{
public override void Shift( int gear )
{
...some specific implementation...
}
}
Use MethodBase.GetCurrentMethod() instead!
Reflection is used to do work with types that can't be done at compile time. Getting the name of the property accessor you're in can be decided at compile time so you probably shouldn't use reflection for it.
You get use the accessor method's name from the call stack using System.Diagnostics.StackTrace though.
string GetPropertyName()
{
StackTrace callStackTrace = new StackTrace();
StackFrame propertyFrame = callStackTrace.GetFrame(1); // 1: below GetPropertyName frame
string properyAccessorName = propertyFrame.GetMethod().Name;
return properyAccessorName.Replace("get_","").Replace("set_","");
}
FWIW I implemented a system like this:
[CrmAttribute("firstname")]
public string FirstName
{
get { return GetPropValue<string>(MethodBase.GetCurrentMethod().Name); }
set { SetPropValue(MethodBase.GetCurrentMethod().Name, value); }
}
// this is in a base class, skipped that bit for clairty
public T GetPropValue<T>(string propName)
{
propName = propName.Replace("get_", "").Replace("set_", "");
string attributeName = GetCrmAttributeName(propName);
return GetAttributeValue<T>(attributeName);
}
public void SetPropValue(string propName, object value)
{
propName = propName.Replace("get_", "").Replace("set_", "");
string attributeName = GetCrmAttributeName(propName);
SetAttributeValue(attributeName, value);
}
private static Dictionary<string, string> PropToAttributeMap = new Dictionary<string, string>();
private string GetCrmAttributeName(string propertyName)
{
// keyName for our propertyName to (static) CrmAttributeName cache
string keyName = this.GetType().Name + propertyName;
// have we already done this mapping?
if (!PropToAttributeMap.ContainsKey(keyName))
{
Type t = this.GetType();
PropertyInfo info = t.GetProperty(propertyName);
if (info == null)
{
throw new Exception("Cannot find a propety called " + propertyName);
}
object[] attrs = info.GetCustomAttributes(false);
foreach (object o in attrs)
{
CrmAttributeAttribute attr = o as CrmAttributeAttribute ;
if (attr != null)
{
// found it. Save the mapping for next time.
PropToAttributeMap[keyName] = attr.AttributeName;
return attr.AttributeName;
}
}
throw new Exception("Missing MemberOf attribute for " + info.Name + "." + propertyName + ". Could not auto-access value");
}
// return the existing mapping
string result = PropToAttributeMap[keyName];
return result;
}
There's also a custom attribute class called CrmAttributeAttribute.
I'd strongly recommend against using GetStackFrame() as part of your solution, my original version of the solution was originally the much neater:
return GetPropValue<string>();
But it was 600x slower than the version above.
Solution # 1
var a = nameof(SampleMethod); //a == SampleMethod
var b = nameof(SampleVariable); //b == SampleVariable
var c = nameof(SampleProperty); //c == SampleProperty
Solution # 2
MethodBase.GetCurrentMethod().Name; // Name of method in which you call the code
MethodBase.GetCurrentMethod().Name.Replace("set_", "").Replace("get_", ""); // current Property
Solution # 3
from StackTrace:
public static class Props
{
public static string CurrPropName =>
(new StackTrace()).GetFrame(1).GetMethod().Name.Replace("set_", "").Replace("get_", "");
public static string CurrMethodName =>
(new StackTrace()).GetFrame(1).GetMethod().Name;
}
you just need to call Props.CurrPropName or Props.CurrMethodName
Solution # 4
Solution for .NET 4.5+:
public static class Props
{
public static string GetCallerName([System.Runtime.CompilerServices.CallerMemberName] String propertyName = "")
{
return propertyName;
}
}
usage: Props.GetCallerName();
Yes, it is!
string test = "test string";
Type type = test.GetType();
PropertyInfo[] propInfos = type.GetProperties();
for (int i = 0; i < propInfos.Length; i++)
{
PropertyInfo pi = (PropertyInfo)propInfos.GetValue(i);
string propName = pi.Name;
}
Try using System.Diagnostics.StackTrace to reflect on the call stack. The property should be somewhere in the call stack (probably at the top if you're calling it directly from the property's code).

Categories