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: Then, how do I get all the properties of one type: I created this test class: The following does not work: 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.
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); }
string
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; }}
var properties = typef (testcook) .GetProperties (bindingflaag.instance | bindingflag.public). Ø = & gt; Ø.CanRead & amp; Ø.CanWrite). Where (ø = & gt; ø.PropertyType == type (string));
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
Post a Comment