Recurse through properties of a class - c#

Take this sample class as an example:
[AttributeUsage(AttributeTargets.All, AllowMultiple=true)]
public class BugFixAttribute : System.Attribute
{
public int BugId { get; private set; }
public string Programmer { get; private set; }
public DateTime Date { get; private set; }
public string Comments { get; set; }
public string RefersTo { get; set; }
public BugFixAttribute(int bugId = 0, string programmer = "")
{
this.BugId = bugId;
this.Programmer = programmer;
Date = DateTime.Now;
}
}
And I want to recuse through the properties to use like:
object[] attr = info.GetCustomAttributes(typeof(BugFixAttribute), false);
foreach (object attribute in attr)
{
BugFixAttribute bfa = (BugFixAttribute) attribute;
Debug.WriteLine(string.Format("\nBugId: {0}", bfa.BugId));
Debug.WriteLine(string.Format("Programmer: {0}", bfa.Programmer));
//...
}
Because what I need to do is to print these to a file. So how can I recurse through the properties instead of doing the Debug.WriteLine() through all of them, is there a way or do I have to write it out.

I would suggest that this is probably not a great use for Attributes, as it muddies up the meta attached to the code. If you want to standardize a way to get at this sort of information regarding bug fixes, I would suggest coming up with an XML Comment Tag, and then turning on XML Comments for your project, and using that instead.
Example Syntax:
/// <summary>This Method Does Something</summary>
/// <BugFix BugId="1234" Programmer="Bob" Date="2/1/2010">Fix Comments</BugFix>
public void MyMethod()
{
// Do Something
}

Yes, if you use reflection:
Type t = bfa.GetType();
PropertyInfo[] properties = t.GetProperties();
foreach(var prop in properties)
{
Debug.WriteLine(string.Format("{0}: {1}", prop.Name,prop.GetValue(bfa,null)));
}
This will print the name and value of all public properties in bfa. You can check the CanRead property on a PropertyInfo to check if it can be read (ie. if it declares a getter). The example will fail if one of the properties are read-only or is indexed - if this can occur, you need to check for it in the code.

I love Linq for this kind of thing
var props = from b in info.GetCustomAttributes(typeof(BugFixAttribute), false)
from p in b.GetType().GetProperties()
select new {
Name = p.Name,
Value = p.GetValue(p.GetValue(b, null))
};
foreach(var prop in props)
{
Debug.WriteLine(string.Format("{0}: {1}", prop.Name, prop.Value));
}

If I read the question correctly, you're looking for a simpler way to write out the class information, right? You've got two options:
reflection, for a highly generic solution that will prolly output way too much information (assuming this is java - or .NET which I've been told is very much the same...)
define the toString() method, and call Debug.WriteLine(bfa)
Solution 1 is probably way overkill. You'll probably get output for stuff you don't want, and you'll not be able to get private values.
Solution 2 is the simple way.
public class BugFixAttribute : System.Attribute
{
...
public String toString(){
return string.Format("\nBugId: {0}\nProgrammer: {1}", this.BugId, this.Programmer));
}
}

foreach (var (BugFixAttribute)attribute in attr)
{
foreach(PropertyInfo prop in attribute.GetType().GetProperties())
{
Debug.WriteLine(string.Format("{0}: {1}", prop.name,prop.GetValue(attribute,null));
}
}

Related

C# Dynamically assign a specific default value to all fields of a certain type in new object instances?

I have a class that contains multiple string fields. Whenever an object of this class is instantiated, I'd like those fields to be automatically assigned with the same specific default value (something like "Undefined"). The reason is:
If I have to serialize the object before all fields are populated with real data, I want those fields to display as this default value rather than being null or string.Empty.
String fields may be added/removed from this class as the project progresses. I'd like to not have to touch the constructor every time that occurs.
Is there any way to do this other than explicitly assigning the default value to each of the string fields one by one in the class constructor?
In C# 6.0 and above, you can use Auto-Property Initializer:
https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-6#auto-property-initializers
Basically:
public string Property { get; set; } = "UNDEFINED";
You would have to use reflection. Something like this
Type type = obj.GetType();
PropertyInfo[] properties = type.GetProperties();
foreach (PropertyInfo property in properties)
{
if (property.PropertyType == typeof(string)) property.setValue(obj, "UNDEFINED");
}
First of all: I don't see how it could be best practice to do what you want.
If you want something like this to show up in your code:
public string Property { get; set; } = "UNDEFINED";
You should probably look into creating custom snippets that simply write exactly that. e.g. https://msdn.microsoft.com/en-us/library/ms165394.aspx
If you don't want that, you could use reflection to find all fields (e.g. strings) in the constructor and set them.
C# Reflection - Get field values from a simple class
FieldInfo[] fields = data.GetType().GetFields(BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.Instance);
Setting a property by reflection with a string value
Ship ship = new Ship();
string value = "5.5";
PropertyInfo propertyInfo = ship.GetType().GetProperty("Latitude");
propertyInfo.SetValue(ship, Convert.ChangeType(value, propertyInfo.PropertyType), null);
Well, why not have an extension method like
public static class MyClass
{
public static string GetDefault(this str, string defaultVal)
{
return string.IsNullOrEmpty(str) ? defaultVal : str;
}
}
For a type
public class SomeClass
{
public string str = string.Empty;
}
You can call
SomeClass s = new SomeClass();
s.str.GetDefault("UNDEFINED");
You can initialize values to fields directly instead of in the constructor.
private string myStringVariable = "UNDEFINED";
Perhaps you should reconsider the structure of your program though if it permits many fields to be initialized to undefined.
Maybe I am misunderstanding this but why not do word for word what you described in the question in your constructor?
public class Weee
{
public string name { get; set; }
public int order { get; set; }
public string whatever { get; set; }
public Weee()
{
foreach(var p in typeof(Weee).GetProperties().Where(a => a.PropertyType == typeof(string)))
{
p.SetValue(this, "wut");
}
}
}
You can create a property initializer and have a base class use it. Your classes can then inherit from the base and have their properties automatically initialized:
public class PropertyInitializer
{
public void Initialize<T>(object obj, T value)
{
PropertyInfo[] properties = obj.GetType().GetProperties();
foreach (PropertyInfo property in properties)
{
if (property.PropertyType == typeof(T))
{
property.SetValue(obj, value);
}
}
}
}
public class InitializedBase
{
protected InitializedBase()
{
var initializer = new PropertyInitializer();
//Initialize all strings
initializer.Initialize<string>(this, "Juan");
//Initialize all integers
initializer.Initialize<int>(this, 31);
}
}
//Sample class to illustrate
public class AutoInitializedClass : InitializedBase
{
public string Name { get; set; }
public int Age { get; set; }
public override string ToString()
{
return string.Format("My name is {0} and I am {1} years old", Name, Age);
}
}
Sample usage:
AutoInitializedClass sample = new AutoInitializedClass();
Console.WriteLine(sample);
Console output:
My name is Juan and I am 31 years old
Notice the base class is using the PropertyInitializer class to initialize fields. This is a simplified example. You can expand it as it fits you (it may not work out of the box with all types).
I personally don't recommend this. It's called a constructor for a reason but you asked a question and I provided an answer.
Here is a simple class from which you can inherit that does exactly what you want:
Example usage:
public class MyClass : DefaultedObject<string>
{
public string MyStringField;
protected override string Default => "UNDEFINED";
}
var myClass = new MyClass();
// myClass.MyStringField == "UNDEFINED"
Implementation:
public abstract class DefaultedObject<T>
{
protected DefaultedObject()
{
T defaultValue = Default;
FieldInfo[] fields = GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
foreach(FieldInfo field in fields) {
if(field.FieldType == typeof(T)) {
field.SetValue(this, defaultValue);
}
}
}
protected abstract T Default { get; }
}
I appreciate all the feedback to this question. Here's what ended up working. First, for any string attributes in the class that I wanted to receive an automatic default value, I established as a property:
public string attribute1 {get; set;}
public string attribute2 {get; set;}
And so on. Then, in the class constructor, I included the following loop which iterates through each property of type string:
foreach(PropertyInfo property in GetType().GetProperties())
{
if (property.PropertyType == typeof(string))
property.SetValue(this, "UNDEFINED"));
}
This produced the desired outcome for me.

Reference known property in (List<someClass>)someObject

have tried some searches. Probably my lack of knowledge that I'm not using the right search terms or perhaps just not understanding the answers.
I have a method that is being passed an object, which I want to output a particular value to a text file.
I already know the object will be a List< someClass > of a few different possible classes (customers/employees/items etc). But all of the classes contain the same string property (e.g.) string idNumber.
So something like this:
public static void OutputFile(object myInput)
{
foreach (someGenericObject in (List<anyType>)myInput)
{
string textToOutput = someGenericObject.idNUmber;
//output the text to somewhere else here
}
}
I feel like as long as I know that it will always contain a this "idNumber" property regardless of the type, that I should be able to reference it somehow. But I just can't seem to get my head around it?
The error I typically get is something like:
Cannot cast List< Employee > to List< object > etc.
Thanks in advance
As I suggested in the comments, if you have the ability to modify these classes, you can have them all inherit from an interface IHasIdNumber with an idNumber property.
Your method would then become:
public static void OutputFile(IEnumerable<IHasIdNumber> myInput)
{
foreach (var item in myInput)
{
string textToOutput = item.idNUmber;
//output the text to somewhere else here
}
}
There are a few ways you can solve this.
Recommended way: Implement common interface:
public interface INumberable { // I'm sure you can come up with a better name...
string IDNumber { get; set; }
}
And then all the possible classes that can be passed into the method will implement INumberable. Your method can then look like this:
public static void OutputFile(List<INumerable> myInput)
{
foreach (var someGenericObject in myInput)
{
string textToOutput = someGenericObject.idNUmber;
//output the text to somewhere else here
}
}
Not-so-recommended way: Reflection:
Type t = someGenericObject.GetType();
var p = t.GetProperty("idNumber");
string theStringYouWant = (string)p.GetValue(someGenericObject);
Note that this is not very safe.
You can use [dynamic].
foreach (var someGenericObject in (dynamic)myInput)
{
//...
}
If all your classes have the same property you want to access in foreach loop you can do in via interface.
public interface ISmth {
int MyProperty { get; set; }
}
public class Student : ISmth {
public int MyProperty { get; set; }
}
public class Employee : ISmth {
public int MyProperty { get; set; }
}
public static void DoSmth(object myObj) {
foreach(ISmth item in (List<object>)myObj) {
Console.Write(item.MyProperty);
}
}
List<Student> stdList = new List<Student>();
DoSmth(stdList.Cast<object>().ToList());

Uri.UnescapeDataString all properties on an object?

I have some objects where some of the properties a URLEncoded.
What would be the fastest way to Uri.UnescapeDataString all properties on an object?
Not clear what you're going to reach. Just assuming you want to automate the process of decode and do not want to call each one property separately and decode it.
The way to iterate through class properties is reflection. Actually, reflection never was fast. But it's also not clear, what you mean by 'fastest'.
Here is a short sample how to achieve it with reflection:
public class DecoderTests
{
public String OneItem { get; set; }
public String SecondItem { get; set; }
public String ThirdClean { get; set; }
}
class Program
{
static void Main(string[] args)
{
var ddd = Uri.EscapeUriString("Http://google tes.com");
var decod = new DecoderTests
{
OneItem = ddd.ToString(),
SecondItem = ddd.ToString(),
ThirdClean = "clean"
};
PropertyInfo[] properties = typeof(DecoderTests).GetProperties();
foreach (PropertyInfo property in properties)
{
var current = property.GetValue(decod) as String;
if (!String.IsNullOrEmpty(current))
{
property.SetValue(decod, Uri.UnescapeDataString(current));
}
}
}
}
Or just call each property separately and decode it. All the alternatives for Uri.UnescapeDataString like Regex.Unescape or HttpUtility.UrlDecode will perform much slower.

System.Reflection - How can I get the PropertyInfo of the sublevel Types?

I have got three classes as follows:
public class TestA
{
public string Str1 { get; set; }
public string Str2 { get; set; }
public List<TestB> LstTestBs { get; set; }
public TestC ObjTestC { get; set; }
}
public class TestB
{
public string Str3 { get; set; }
public string Str4 { get; set; }
}
public class TestC
{
public string Str5 { get; set; }
}
I have tried:
var prop = typeof (TestA).GetProperties();
But, it is giving only the PropertyInfo for the four members inside TestA. I need to get the PropertyInfo for all the members in the TestA, TestB and TestC classes.
Please help...
Thanks in advance,
San
If you put all your classes in the same namespace, you can collect the properties by enumerating the classes in the namespace, instead of mining the property structure:
Getting all types in a namespace via reflection
Thanks for the help everyone.
I have got the answer.
var prop = typeof (TestA).GetProperties();
for (int i=0;i<prop.Count();i++)
{
var propertyInfo = prop[i];
if (propertyInfo.PropertyType.Namespace != "System")
{
if (propertyInfo.PropertyType.IsGenericType &&
propertyInfo.PropertyType.GetGenericTypeDefinition() == typeof (List<>))
{
Type itemType = propertyInfo.PropertyType.GetGenericArguments()[0];
var listObjectProperties = itemType.GetProperties();
prop = prop.Union(listObjectProperties).ToArray();
}
else
{
var childProp = propertyInfo.PropertyType.GetProperties();
prop = prop.Union(childProp).ToArray();
}
}
}
SLaks is right. You should do this recursively. See wikipedia's article on Recursion for more information on the concept. For example, in your case, this is the general idea:
public void AddPropertiesAndChildPropertiesToList(Type type, List<PropertyInfo> list)
{
var properties = type.GetProperties();
list.AddRange(properties);
foreach (var property in properties)
{
// recursive methods are ones that call themselves, like this...
AddPropertiesAndChildPropertiesToList(property.PropertyType, list);
}
}
Note that this example is lacking several things:
Most importantly, it has no guard against infinite recursion. You could fix this by keeping track of where you'd already been with a Stack<Type> alreadyVisited parameter. If you find you've been asked to add the list of properties for a type you've already visited, just return out of the method instead, or throw an exception.
As I mentioned in your other related question, for your purposes you really need to be keeping track of property chains, not just properties. The alreadyVisited stack would be useful here, too.
It won't handle your List<TestB> in any useful way. For that, you probably need to figure out whether the type has an indexer, and then the properties of the type that is returned by that indexer.

Get collection property of a specific type

I have a class MyDatabaseContext that has a series of DbSet collection properties:
public DbSet<EntityA> EntitiesA { get; set; }
public DbSet<EntityB> EntitiesB { get; set; }
public DbSet<EntityC> EntitiesC { get; set; }
I need to get the name of the collection given the type of the entity.
For example, I have "EntityB" and want to get as a result "EntitiesB".
I really wanted to avoid switch-case statements, since MyDatabaseContext is generated automatically (T4 templates).
if you just want the name of the property here you go. I would just refine the answer given by hunter. You can use the same method with string as return type.
public string GetEntitiName<T>() where T : class
{
PropertyInfo propInfo = typeof(MyDatabaseContext).GetProperties().Where(p => p.PropertyType == typeof(DbSet<T>)).FirstOrDefault();
string propertyName = propInfo.Name; //The string has the property name ..
return propertyName;
}
I tried a sample similar to your situation. Try replacing List with DbSet.
class Program
{
public static void GetEntities<T>() where T : class
{
var info = typeof(TestClass1).GetProperties().Where(p => p.PropertyType == typeof(List<T>));
Console.WriteLine(info.FirstOrDefault().Name);
}
static void Main(string[] args)
{
GetEntities<int>();
Console.ReadLine();
}
}
public class TestClass1
{
public List<int> IntTest { get; set; }
public List<double> DoubleTest { get; set; }
public List<string> IStringTest { get; set; }
}
This sample works.
I know this is old page, But my answer maybe useful for other guys referring here. (like me)
I think you want to accessing EntitiesB to run a query on it, like EntitiesB.Where(a=>a.bla=="blabla"). If I'm right or another visitor of this page needs something like this, just easily use the following code:
using System.Data.Entity.Infrastructure;
using System.Data.Objects;
((IObjectContextAdapter)_dbContext).ObjectContext.CreateObjectSet<EntityB>()
Description:
_dbContext is Context class inherting from DbContext.
EntitiesB is DbSet<EntityB> defined in Context class.
Example:
Ilist result = ((IObjectContextAdapter)_dbContext).ObjectContext.CreateObjectSet<EntityB>().Where(b=>b.bla=="blabla").ToList();
Your generated file is a partial class, you could create a new file and declare a class with same name using the keyword partial, then make a method which will return the desired Collection...
I haven't actually done this myself, but it sounds like what you want to do is to use reflection to locate the property of type "DbSet" that has the appropriate generic type parameter. The following pseudo-C# should get you started:
foreach ( FieldInfo field in this.GetType() )
{
if ( field.FieldType.IsGenericType )
{
foreach ( Type param in field.FieldType.GetGenericArguments() )
{
if ( param.Name == soughtType )
{
return field.Name;
}
}
}
}

Categories