I am examining an object via reflection, and I want to print those properties which have non-default values. Given a signature like this: public static bool IsDefaultValue(PropertyInfo prop, object o) {  //returns true if the given property's value on o is the default value for the property type }  How could this be implemented?
5349
I'm using reflection to loop through a Type's properties and set certain types to their default.  Now, I could do a switch on the type and set the default(Type) explicitly, but I'd rather do it in one line.  Is there a programmatic equivalent of default?
54835
I'm used to writing classes like this:  public class foo {   private string mBar = "bar";   public string Bar {     get { return mBar; }     set { mBar = value; }   }   //... other methods, no constructor ... }   Converting Bar to an auto-property seems convenient and concise, but how can I retain the initialization without adding a constructor and putting the initialization in there?  public class foo2theRevengeOfFoo {   //private string mBar = "bar";   public string Bar { get; set; }   //... other methods, no constructor ...   //behavior has changed. }   You could see that adding a constructor isn't inline with the effort savings I'm supposed to be getting from auto-properties.  Something like this would make more sense to me:  public string Bar { get; set; } = "bar";
90316
Is there a way in C# where I can use reflection to set an object property?  Ex:  MyObject obj = new MyObject(); obj.Name = "Value";   I want to set obj.Name with reflection. Something like:  Reflection.SetProperty(obj, "Name") = "Value";   Is there a way of doing this?
15188
Say I create an object thus:  var myObject =         {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"};   What is the best way to retrieve a list of the property names? i.e. I would like to end up with some variable 'keys' such that:  keys == ["ircEvent", "method", "regex"]
24836
Given a method signature:  public bool AreTheSame&lt;T&gt;(Expression&lt;Func&lt;T, object&gt;&gt; exp1, Expression&lt;Func&lt;T, object&gt;&gt; exp2)   What would be the most efficient way to say if the two expressions are the same? This only needs to work for simple expressions, by this I mean all that would be "supported" would be simple MemberExpressions, eg c => c.ID.  An example call might be:  AreTheSame&lt;User&gt;(u1 =&gt; u1.ID, u2 =&gt; u2.ID); --&gt; would return true
88076
This is what I've come up with as a method on a class inherited by many of my other classes. The idea is that it allows the simple comparison between properties of Objects of the same Type.  Now, this does work - but in the interest of improving the quality of my code I thought I'd throw it out for scrutiny. How can it be better/more efficient/etc.?  /// &lt;summary&gt; /// Compare property values (as strings) /// &lt;/summary&gt; /// &lt;param name="obj"&gt;&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; public bool PropertiesEqual(object comparisonObject) {      Type sourceType = this.GetType();     Type destinationType = comparisonObject.GetType();      if (sourceType == destinationType)     {         PropertyInfo[] sourceProperties = sourceType.GetProperties();         foreach (PropertyInfo pi in sourceProperties)         {             if ((sourceType.GetProperty(pi.Name).GetValue(this, null) == null &amp;&amp; destinationType.GetProperty(pi.Name).GetValue(comparisonObject, null) == null))             {                 // if both are null, don't try to compare  (throws exception)             }             else if (!(sourceType.GetProperty(pi.Name).GetValue(this, null).ToString() == destinationType.GetProperty(pi.Name).GetValue(comparisonObject, null).ToString()))             {                 // only need one property to be different to fail Equals.                 return false;             }         }     }     else     {         throw new ArgumentException("Comparison object must be of the same type.","comparisonObject");     }      return true; }
70095
I want to be able to debug C structures without having to explicitly type every property that they consist of.  i.e. I want to be able to do something like this:  CGPoint cgPoint = CGPointMake(0,0); NSLog(@"%@",cgPoint);   Obviously the '%@' won't work, hence the question.
78703
If have this in the setter of a property:  decimal? temp = value as decimal?;   value = "90"  But after the cast, temp is null...  What is the proper way to do this cast?
69100
As an example take the following code:  public enum ExampleEnum { FooBar, BarFoo }  public class ExampleClass : INotifyPropertyChanged {     private ExampleEnum example;      public ExampleEnum ExampleProperty      { get { return example; } { /* set and notify */; } } }   I want a to databind the property ExampleProperty to a ComboBox, so that it shows the options "FooBar" and "BarFoo" and works in mode TwoWay. Optimally I want my ComboBox definition to look something like this:  &lt;ComboBox ItemsSource="What goes here?" SelectedItem="{Binding Path=ExampleProperty}" /&gt;   Currently I have handlers for the ComboBox.SelectionChanged and ExampleClass.PropertyChanged events installed in my Window where I do the binding manually.  Is there a better or some kind of canonical way? Would you usually use Converters and how would you populate the ComboBox with the right values? I don't even want to get started with i18n right now.  Edit  So one question was answered: How do I populate the ComboBox with the right values.  Retrieve Enum values as a list of strings via an ObjectDataProvider from the static Enum.GetValues method:  &lt;Window.Resources&gt;     &lt;ObjectDataProvider MethodName="GetValues"         ObjectType="{x:Type sys:Enum}"         x:Key="ExampleEnumValues"&gt;         &lt;ObjectDataProvider.MethodParameters&gt;             &lt;x:Type TypeName="ExampleEnum" /&gt;         &lt;/ObjectDataProvider.MethodParameters&gt;     &lt;/ObjectDataProvider&gt; &lt;/Window.Resources&gt;   This I can use as an ItemsSource for my ComboBox:  &lt;ComboBox ItemsSource="{Binding Source={StaticResource ExampleEnumValues}}"/&gt;
85842
Assuming the following hypothetical inheritance hierarchy:  public interface IA {   int ID { get; set; } }  public interface IB : IA {   string Name { get; set; } }   Using reflection and making the following call:   typeof(IB).GetProperties(BindingFlags.Public | BindingFlags.Instance)    will only yield the properties of interface IB, which is "Name".   If we were to do a similar test on the following code,  public abstract class A {   public int ID { get; set; } }  public class B : A {   public string Name { get; set; } }   the call typeof(B).GetProperties(BindingFlags.Public | BindingFlags.Instance) will return an array of PropertyInfo objects for "ID" and "Name".  Is there an easy way to find all the properties in the inheritance hierarchy for interfaces as in the first example?
22388
How do you give a C# auto-property an initial value?  I either use the constructor, or revert to the old syntax.    Using the Constructor:  class Person  {     public Person()     {         Name = "Initial Name";     }     public string Name { get; set; } }   Using normal property syntax  (with an initial value)  private string name = "Initial Name"; public string Name  {     get      {         return name;     }     set     {         name = value;     } }   Is there a better way?
7345
I have a configuration file where a developer can specify a text color by passing in a string:   &lt;text value="Hello, World" color="Red"/&gt;   Rather than have a gigantic switch statement look for all of the possible colors, it'd be nice to just use the properties in the class System.Drawing.Brushes instead so internally I can say something like:   Brush color = Brushes.Black;   // Default   // later on...  this.color = (Brush)Enum.Parse(typeof(Brush), prasedValue("color"));   Except that the values in Brush/Brushes aren't enums.  So Enum.Parse gives me no joy.  Suggestions?
26112
Given this class  class Foo {     // Want to find _bar with reflection     [SomeAttribute]     private string _bar;      public string BigBar     {         get { return this._bar; }     } }   I want to find the private item _bar that I will mark with a attribute.  Is that possible?    I have done this with properties where I have looked for an attribute, but never a private member field.  What are the binding flags that I need to set to get the private fields?
17511
So this seems pretty basic but I can't get it to work.  I have an Object, and I am using reflection to get to it's public properties.  One of these properties is static and I'm having no luck getting to it.  Public Function GetProp(ByRef obj As Object, ByVal propName as String) as PropertyInfo     Return obj.GetType.GetProperty(propName)  End Function   The above code works fine for Public Instance properties, which up until now is all that I have needed.  Supposedly I can use BindingFlags to request other types of properties (private, static), but I can't seem to find the right combination.  Public Function GetProp(ByRef obj As Object, ByVal propName as String) as PropertyInfo     Return obj.GetType.GetProperty(propName, Reflection.BindingFlags.Static Or Reflection.BindingFlags.Instance Or Reflection.BindingFlags.Public)  End Function   But still, requesting any Static members return nothing.  .NET reflector can see the static properties just fine, so clearly I am missing something here.
63369
Does any one know how I can specify the Default value for a DateTime property using the System.ComponentModel DefaultValue Attribute?  for example I try this:  [DefaultValue(typeof(DateTime),DateTime.Now.ToString("yyyy-MM-dd"))] public DateTime DateCreated { get; set; }   And it expects the value to be a constant expression.  This is in the context of using with ASP.NET Dynamic Data.  I do not want to scaffold the DateCreated column but simply supply the DateTime.Now if it is not present.  I am using the Entity Framework as my Data Layer  Cheers,  Andrew
82914
I have some dynamic SOQL code in which I'd like to identify whether a method argument is a collection or a single value:  SObject[] query(SObjectType sobType, String field, Object data) {     // Want to know if data is a collection e.g. set or list     // that requires an "in" or a single value that requires     // an "=" in the query. (May also need to use separate bind variables.) }   I note that the sort of check shown immediately below seems to require an exact match for the type of the collection (Id in this case to return true) which is not what I want:  Object o = new Set&lt;Id&gt;(); System.debug(o instanceof Set&lt;Id&gt;);   Is there a simple way to make this determination?  (Yes there are various other ways to code this; checking first to see if there is a direct solution.)  PS  Looks like great caution is needed when using .
53020
I have a question about default constructors and inheritance in Java.   Generally, if you write a class and do not include any constructor, Java provides automatically for you a default constructor (one without parameters), which initializes all instance variables of the class (if there are any) with some default values (0, null, or false). If you write a constructor, however, with some parameters, and you don't write  any default constructor, then Java does not provide a default constructor.  My question is: what is the case with classes, which inherit from other classes - if I write a constructor with some parameters in them, but don't include a default constructor, do they inherit the default constructor of the super class?