Work with missing MI in C #

I have code that gets a class derived from a specific class. Let me name this class of parameters.
The code uses reflection to go to class members and analyze their attributes. Basically, it is a custom analyzer that will parse input data according to attributes and put what it finds into data members.

This is used in several places in our code. You specify a parameter class, add attribute data elements and pass it to the parser. Something like that:

public class MyFancyParameters : ParametersBase
{
  [SomeAttribute(Name="blah", AnotherParam=true)]
  public string Blah { get; set; }

  // .. .more such stuff
}

var parameters = new MyFancyParameters();
Parser.Parse(input, parameters);

, . , . , , . , , , . , .

, .
, ++, - , , , , . ( , .)
# . , . .

?

+3
1

? . , ParametersBase, ParametersBase, .

, , . , , , , . , .


, "" , . , (, ).

public class ParametersParser
{
    public static IEnumerable<PropertyInfo> GetAllParameterProperties(Type parameterType)
    {
        foreach (var property in parameterType.GetProperties())
        {
            if (Attribute.IsDefined(property, typeof(SomeAttribute)))
                yield return property;

            if (typeof(ParametersBase).IsAssignableFrom(property.PropertyType))
            {
                foreach (var subProperty in GetAllParameterProperties(property.PropertyType))
                    yield return subProperty;
            }
        }
    }
}
+1

Source: https://habr.com/ru/post/1756004/


All Articles