   [.net] Reflection on Properties
  Hey folks,I ran into a problem I am hoping you guys can help me out again! How about some sorce to visualize what I am trying to do:class Foo  {  public Int32 Bar { get; internal set; }  public Foo()    {    typeof(Foo).GetProperty("Bar").GetSetMethod().Invoke(this, new Object[] {123});    }  }As you can see I am trying to assign the property Bar a value through reflection. Compiling this is not the issue, however an exception is thrown as soon as the code is reached. Two questions:1. Why isn't this working?2. What can I do to make it work? I do not want to make the Set Method of Bar public, which would sort out the problem. Is there another way to Invoke the Set Method?Thx in advance!!cherio Woltan   
  When you remove the "internal", it will work.   ;  Hi phresnel,thx for your posting. I know that if it is public it will work, but I realy realy do need that internal there. In fact I'd rather have it "protected" from the bad world outside^^cherio Woltan   ;  You really should be doing each operation on a separate line so that you can find out which one is causing the error:    public Foo()    { Type t = typeof(Foo); var propInfo = t.GetProperty("Bar"); var methodInfo = propInfo.GetSetMethod(); methodInfo.Invoke(this, new Object[] {123});    }In this case, the problem is that GetSetMethod(), by default, only looks for public methods. You need to use the overload that takes a boolean for whether it should look at non-public methods as well.I'm not sure what you're trying to do, though. In the code you've posted, you can just use the setter directly. That is, this.Bar = 123; will work.   ;  Better yet, GetSetMethod() has an overload that accepts a boolean with which you tell GetSetMethod() whether it is allowed to call/get/set private/internal members.class Foo {   public Int32 Bar { get; internal set; }   public Foo () { typeof (Foo).    GetProperty ("Bar").    GetSetMethod (true).  /* there */    Invoke (this, new Object [] { 123 });   }    }edit: ninja'd 38secs   ;  Great, that does the trick. Long live the overloading!!thx for your help!cheiro Woltan   