29
Practical Reflection Presented by Jeremy Clark www.jeremybytes.com

Introduction to XAML with WPFjeremybytes.com/Downloads/Slides-PracticalReflection.pdf · Introduction to XAML with WPF Author: Jeremy Created Date: 11/15/2016 8:02:41 PM

  • Upload
    others

  • View
    35

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Introduction to XAML with WPFjeremybytes.com/Downloads/Slides-PracticalReflection.pdf · Introduction to XAML with WPF Author: Jeremy Created Date: 11/15/2016 8:02:41 PM

Practical Reflection

Presented by Jeremy Clarkwww.jeremybytes.com

Page 2: Introduction to XAML with WPFjeremybytes.com/Downloads/Slides-PracticalReflection.pdf · Introduction to XAML with WPF Author: Jeremy Created Date: 11/15/2016 8:02:41 PM

Just for Experts?

Page 3: Introduction to XAML with WPFjeremybytes.com/Downloads/Slides-PracticalReflection.pdf · Introduction to XAML with WPF Author: Jeremy Created Date: 11/15/2016 8:02:41 PM

Goal

@jeremybytes

Page 4: Introduction to XAML with WPFjeremybytes.com/Downloads/Slides-PracticalReflection.pdf · Introduction to XAML with WPF Author: Jeremy Created Date: 11/15/2016 8:02:41 PM

What is Reflection?

What is an assembly?

What is metadata?

How is the code compiled?

@jeremybytes

Inspecting the metadata and code in an assembly.

Page 5: Introduction to XAML with WPFjeremybytes.com/Downloads/Slides-PracticalReflection.pdf · Introduction to XAML with WPF Author: Jeremy Created Date: 11/15/2016 8:02:41 PM

.NET Assemblies

@jeremybytes

AssemblyManifest

Metadata+ IL

Resources(optional)

Page 6: Introduction to XAML with WPFjeremybytes.com/Downloads/Slides-PracticalReflection.pdf · Introduction to XAML with WPF Author: Jeremy Created Date: 11/15/2016 8:02:41 PM

Type Definitions

@jeremybytes

Page 7: Introduction to XAML with WPFjeremybytes.com/Downloads/Slides-PracticalReflection.pdf · Introduction to XAML with WPF Author: Jeremy Created Date: 11/15/2016 8:02:41 PM

Assembly Information

@jeremybytes

Page 8: Introduction to XAML with WPFjeremybytes.com/Downloads/Slides-PracticalReflection.pdf · Introduction to XAML with WPF Author: Jeremy Created Date: 11/15/2016 8:02:41 PM

Referenced Assemblies

@jeremybytes

Page 9: Introduction to XAML with WPFjeremybytes.com/Downloads/Slides-PracticalReflection.pdf · Introduction to XAML with WPF Author: Jeremy Created Date: 11/15/2016 8:02:41 PM

IL (Intermediate Language)

@jeremybytes

Page 10: Introduction to XAML with WPFjeremybytes.com/Downloads/Slides-PracticalReflection.pdf · Introduction to XAML with WPF Author: Jeremy Created Date: 11/15/2016 8:02:41 PM

Feature Overview

@jeremybytes

Activator

Type Assembly

ILGenerator

••

Page 11: Introduction to XAML with WPFjeremybytes.com/Downloads/Slides-PracticalReflection.pdf · Introduction to XAML with WPF Author: Jeremy Created Date: 11/15/2016 8:02:41 PM

Things You Can Do

@jeremybytes

Page 12: Introduction to XAML with WPFjeremybytes.com/Downloads/Slides-PracticalReflection.pdf · Introduction to XAML with WPF Author: Jeremy Created Date: 11/15/2016 8:02:41 PM

Things You Can Do

@jeremybytes

Page 13: Introduction to XAML with WPFjeremybytes.com/Downloads/Slides-PracticalReflection.pdf · Introduction to XAML with WPF Author: Jeremy Created Date: 11/15/2016 8:02:41 PM

Things You Can Do

@jeremybytes

Page 14: Introduction to XAML with WPFjeremybytes.com/Downloads/Slides-PracticalReflection.pdf · Introduction to XAML with WPF Author: Jeremy Created Date: 11/15/2016 8:02:41 PM

Encapsulation

@jeremybytes

Input

Output

Page 15: Introduction to XAML with WPFjeremybytes.com/Downloads/Slides-PracticalReflection.pdf · Introduction to XAML with WPF Author: Jeremy Created Date: 11/15/2016 8:02:41 PM

DEMO

Performance Concerns

Page 16: Introduction to XAML with WPFjeremybytes.com/Downloads/Slides-PracticalReflection.pdf · Introduction to XAML with WPF Author: Jeremy Created Date: 11/15/2016 8:02:41 PM

Best Practice

@jeremybytes

Page 17: Introduction to XAML with WPFjeremybytes.com/Downloads/Slides-PracticalReflection.pdf · Introduction to XAML with WPF Author: Jeremy Created Date: 11/15/2016 8:02:41 PM

Practical Reflection Strategy

• Dynamically Load Assemblies• Happens one time (at start up)

• Dynamically Load Types• Happens one time (at start up)

• Cast Types to a Known Interface• All method calls go through the interface

• No dynamic method calls – no MethodInfo.Invoke

• Avoid interacting with private members

@jeremybytes

Page 18: Introduction to XAML with WPFjeremybytes.com/Downloads/Slides-PracticalReflection.pdf · Introduction to XAML with WPF Author: Jeremy Created Date: 11/15/2016 8:02:41 PM

Various Data Sources

@jeremybytes

MongoDB

Amazon AWS

Microsoft Azure

WebAPI

CSV SOAP Service

Microsoft SQL Server

Oracle

JSON Hadoop

Page 19: Introduction to XAML with WPFjeremybytes.com/Downloads/Slides-PracticalReflection.pdf · Introduction to XAML with WPF Author: Jeremy Created Date: 11/15/2016 8:02:41 PM

Pluggable Repositories

@jeremybytes

WCF Service

Repository

CSV File

Repository

SQL Database

Repository

Application

Page 20: Introduction to XAML with WPFjeremybytes.com/Downloads/Slides-PracticalReflection.pdf · Introduction to XAML with WPF Author: Jeremy Created Date: 11/15/2016 8:02:41 PM

DEMO

Run-Time Binding

Page 21: Introduction to XAML with WPFjeremybytes.com/Downloads/Slides-PracticalReflection.pdf · Introduction to XAML with WPF Author: Jeremy Created Date: 11/15/2016 8:02:41 PM

Benefits of Dynamic Loading

@jeremybytes

Page 22: Introduction to XAML with WPFjeremybytes.com/Downloads/Slides-PracticalReflection.pdf · Introduction to XAML with WPF Author: Jeremy Created Date: 11/15/2016 8:02:41 PM

Assembly-Qualified Type Name

• Fully-qualified type name (namespace and type)

• Assembly Name

• Assembly Version

• Assembly Culture

• Assembly Public Key (for strongly-named assemblies)

@jeremybytes

Page 23: Introduction to XAML with WPFjeremybytes.com/Downloads/Slides-PracticalReflection.pdf · Introduction to XAML with WPF Author: Jeremy Created Date: 11/15/2016 8:02:41 PM

Limiting Reflection

private void FetchButton_Click(object sender, EventArgs e)

{

ClearListBox();

var people = repository.GetPeople();

foreach (var person in people)

PersonListBox.Items.Add(person);

ShowRepositoryType(repository);

}

No Reflection Here

Method calls through

IPersonRepository

@jeremybytes

Page 24: Introduction to XAML with WPFjeremybytes.com/Downloads/Slides-PracticalReflection.pdf · Introduction to XAML with WPF Author: Jeremy Created Date: 11/15/2016 8:02:41 PM

ScenarioClient #1

@jeremybytes

BusinessRuleBusinessRuleBusinessRule

Order EntryApplication

Client #2BusinessRuleBusinessRuleBusinessRule

Client #3BusinessRuleBusinessRuleBusinessRule

Page 25: Introduction to XAML with WPFjeremybytes.com/Downloads/Slides-PracticalReflection.pdf · Introduction to XAML with WPF Author: Jeremy Created Date: 11/15/2016 8:02:41 PM

Application

@jeremybytes

Page 26: Introduction to XAML with WPFjeremybytes.com/Downloads/Slides-PracticalReflection.pdf · Introduction to XAML with WPF Author: Jeremy Created Date: 11/15/2016 8:02:41 PM

Business Rule Interface

@jeremybytes

Page 27: Introduction to XAML with WPFjeremybytes.com/Downloads/Slides-PracticalReflection.pdf · Introduction to XAML with WPF Author: Jeremy Created Date: 11/15/2016 8:02:41 PM

Business Rules

@jeremybytes

Maximum Discount based

onCustomer Rating

Maximum of 1 Starshipper Order

Only 1Captain’s

ChairAllowed

Name Badgemust match

Customer Name

Page 28: Introduction to XAML with WPFjeremybytes.com/Downloads/Slides-PracticalReflection.pdf · Introduction to XAML with WPF Author: Jeremy Created Date: 11/15/2016 8:02:41 PM

Discovery Process

• Locate all assemblies in the “Rules” folder

• Load each assembly

• Enumerate the types in the assembly

• Check each type to see if it implements our Rule interface

• Create an instance of each Rule and add it to the Rule Catalog

@jeremybytes

Page 29: Introduction to XAML with WPFjeremybytes.com/Downloads/Slides-PracticalReflection.pdf · Introduction to XAML with WPF Author: Jeremy Created Date: 11/15/2016 8:02:41 PM

Thank You!

Jeremy Clark

•http://www.jeremybytes.com

[email protected]

•@jeremybytes