Reflection in C #?

I recently made the switch from a Java web developer to a C # application developer who mainly uses WPF applications. I used Spring MVC with Java, where most of the code structure was unloaded and configured for me. Since I switched to WPF, my applications depend on my ability to customize reusable, untied code.

I am trying to improve my skills in certain areas of design, including Generics, Design Patterns and Reflection.

I well know that this is all for the Generics and Design models, I pretty well use what I consider to be the best (although this is in the air).

What I do not know too well is a reflection. I know what reflection is, and I know how to do this in order to perform tasks such as dynamically loading an assembly, calling a method and class, etc.

What I do not understand are examples of how this can help me.

I constantly hear how reflection can be really useful if you know how to use it. When I try to research a topic, I find only tutorials and recommendations on how to do this, and not what you can use for.

My question is what can I consider as a WPF developer to use reflection for this, which will help me and / or there is a place or link that can give me more than just the syntax of using reflection, but also real world examples and / or best practices?

+5
source share
8 answers

: , , ; .

, . #, Texas Hold'em. , , , , . , , GUI, , . , .

, , , . .

, , , . , CPUAttribute.

, : , . , .

+3

, :

  • ORM ( ):
  • : , -
  • : - DTO
  • : json xml
  • MVC: URL-
  • :

:

public class Customer : Entity
{
    [Required]
    public string Name { get; set; }
}

- , , :

public class Validator
{
    public ValidationResult Validate(Entity entity)
    {
        // use reflection to find validation attributes and enforce them
    }
}
+4

- API. :

  • ,
  • , ( )

WPF -, . API , ( DependencyObject, ..), - ( ViewModel).

, ,

{   public string {get; ; } }

WPF () Customer , Name. XAML, :

var value = dataObject.GetType().GetProperty("Name").GetValue(dataObject, null);

, , . :

dataObject.GetType().GetProperty("Name").SetValue(dataObject, "Bob", null);

"Bob", .

, , . , , , , ; ( , #) , . , , , .

, , , , , - .

+4

- , ( )

/ . , , , . .

:

//Without reflection
Foo foo = new Foo();
foo.Hello();

//With reflection
object foo = Activator.CreateInstance(null, "Foo");
foo.GetType().GetMethod("Hello").Invoke(foo, null);

. , , X Y . X Y. , , , X Y . - , , , . , .

:

MSDN. .

, Obfuscation .

+2

, . WPF, ; , xaml ? xaml?

:

  • ORM - < === > classes
  • IDE/ /

WPF 3 ; , 4,

; .

+2

. , . , Employe, .

, , , , .

.

, , , , , , clonig.

, , .

+1

, , , - ( ) , , .

, , - XMLSerializer/Deserializer, II , ( ) .

, , , - !

+1

, . , , , , , .

, DLL- . , , , dll .

Another use case (abuse?) Is to bypass encapsulation and set up a private field in another object when you know the field name, although if you do, you should seriously doubt it is worth doing it.

+1
source

All Articles