reflection - C#: How to get all public (both get and set) string properties of a type -


I am trying to create a method that will go through a list of general objects and all their properties

What is a good way to do this?

I have this kind of ... Shell ... So far:

  Change the public static zero: pststressing & lt; T & gt; (List & lt; T & gt; List, String Replacement) {var Properties = Typef (T) .GetProperties (- What BindFlag? -); Foreign Currency (Various Points in Properties) {foreach (different items in the list) {if (string.IsNullOrEmpty (string) p.GetValue (item, blank)) p.SetValue (item, replacement, blank); }  

Then, how do I get all the properties of one type:

  • Type string get < li>

    I created this test class:

      class TestSubject {public string public; Private string private; Public String Public Lok Sabha {Received; Set; } Public String Public Private {Received; Private set; } Public string PrivatePublic {Private find; Set; } Private string PrivatePrivate {get; Set; }}  

    The following does not work:

      var properties = typef (testcook) .GetProperties (bindingflaag.instance | bindingflag.public). Ø = & gt; Ø.CanRead & amp; Ø.CanWrite). Where (ø = & gt; ø.PropertyType == type (string));  

    If I can print the names of the properties that meet me there, I get it:

    Public public private private personal people < / P>

    In other words, I get two properties too much


    Note : This may be done better Can ... using nested forward and reflection and all here ... but if you have any great alternative ideas, please Uje know that I want to learn!

    Your code is rewritten. Does not use LINQ or neither.

      Public Static Zero ReplacingEmptyStrings & lt; T & gt; (List & lt; T & gt; List, String replacement) {PropertyInfo [] Properties = Type (T) .GetProperties (BindingFlags.Public | BondFlag.Instance); Foreach (property inforce in properties) {// only work with strings if (P. property type! = Typef (string)) {continue; } // if it is not writable, it can not be blank; If not readable, then it can not be checked if (! P.CanWrite ||! P.CanRead) {continue; } MethodInfo mget = p.GetGetMethod (false); MethodInfo mset = p.GetSetMethod (wrong); // Receive and make the methods public if (mAgt == empty) {continue; } If (mset == faucet) {continue; } Foreach (T items in the list) {if (string.IsNullOrEmpty (string) p.GetValue (item, blank)) {p.SetValue (item, replacement, blank); }}}}  

Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -