Mateusz Stanek

Refleksja – odczyt metadanych

Refleksja

Refleksja jest mechanizmem umożliwiającym odczytywanie metadanych. Dzięki niemu możemy odczytać atrybuty i własności obiektów w bibliotekach. Dzięki temu mechanizmowi możemy bez trudu odczytywać własne atrybuty tworzone jak w poprzednim wpisie https://mateuszstanek.pl/2018/02/04/wlasny-atrybut/

Ładowania biblioteki

Informację o bibliotece można pozyskać na kilka sposobów. W zależności jakie assembly chcemy załadować. Do wyboru mamy kilka metod dostępnych w klasie Assembly. Kilka z dostępnych metod to :

  • GetAssembly – która jako parametr przyjmuje typ klasy – zwraca nam assembly, które ten typ zawiera
  • GetExecutingAssembly – zwraca assembly, które jest aktualnie wykonywane, czyli to assembly, w którym znajduje się kod wywołujący zapytanie
  • GetCallingAssembly – zwraca assembly, które wywołało metodę, w której wywołujemy zapytanie
  • GetEntryAssembly – zwraca assembly, którego proces jest wykonywany
  • LoadFrom – która jako parametr przyjmuje ścieżkę do biblioteki – zwraca nam assembly tego właśnie pliku

C#
1
2
3
4
5
var assembly = Assembly.LoadFrom("AdditionalLibrary.dll");
var assembly2 = Assembly.GetExecutingAssembly();
var assembly3 = Assembly.GetEntryAssembly();
var assembly4 = Assembly.GetCallingAssembly();
var assembly5 = Assembly.GetAssembly(typeof(SomeAdditionalClass));

Po załadowaniu Assembly, możemy odczytać takie informacje jak atrybuty całego assemby, a także dostępne Typy danych.

Operacje na typach

Kiedy dostaniemy interesujący nas typ danych również możemy odczytać jego metadane. Refleksja umożliwia nam odczyt informacji na temat konstruktorów, pól, właściwości oraz metod.

Poniższy przykład obrazuje jak załadować i odczytać wszystkie właściwości i typy z danego assembly.

C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System;
using System.Reflection;
 
namespace ReflectionAssemblyRead
{
    class Program
    {
        static void Main(string[] args)
        {
            var assembly = Assembly.LoadFrom("ExternalLibrary.dll");
            foreach (var atribute in assembly.CustomAttributes)
            {
                Console.WriteLine($"{atribute.ToString()}");
            }
            Console.WriteLine();
            foreach (var type in assembly.ExportedTypes)
            {
                Console.WriteLine(type.ToString());
                Console.WriteLine("Constructors");
                foreach (var ctrs in type.GetConstructors())
                {
                    Console.WriteLine($"\t {ctrs.ToString()}");
                    foreach (var param in ctrs.GetParameters())
                    {
                        Console.WriteLine($"\t\t parmeter {param.Name} : {param.ParameterType}");
                    }
                }
                Console.WriteLine("Properties");
                foreach (var prop in type.GetProperties())
                {
                    Console.WriteLine($"\t {prop.ToString()}");
                }
                Console.WriteLine("Fields");
                foreach (var prop in type.GetFields())
                {
                    Console.WriteLine($"\t {prop.ToString()}");
                }
 
                Console.WriteLine("Methods");
                foreach (var method in type.GetMethods())
                {
                    Console.WriteLine($"\t {method.ToString()}");
                    foreach(var param in method.GetParameters())
                    {
                        Console.WriteLine($"\t\t parmeter {param.Name} : {param.ParameterType}");
                    }
                    Console.WriteLine($"\t\t return : {method.ReturnType}");
                }
            }
            Console.ReadLine();
        }
    }
}

 

Jak widzimy odczyt metadanych jest prosty, kompletny projekt możemy znaleść na moim GitHub-ie https://github.com/Matejkos/BlogProjects/

W następnym poście postaram się opowiedzieć, o tym jak za pomocą refleksji tworzyć obiekty, zmieniać ich właściwości czy też wywoływać metody.

 

Related posts:

  1. Własny Atrybut
  2. Atrybuty w C#
  3. Czytelne testy z FluentAssertions
  4. Przetwarzanie kolekcji z Linq to Object cz1
5 lutego 2018 MiroBurn Challange Programowanie
No Comments

Własny Atrybut

Refleksja - manipulacja obiektami

Dodaj komentarz Anuluj pisanie odpowiedzi

Zobacz

  • Mateusz Stanek o mnie
Mateusz Stanek

Zaprzyjaźnione blogi

SGDev.pl

Ostatnie wpisy

  • Rola Prelegent
  • Elektronika – powrót do starego hobby
  • Badger2040
  • 2023 Tydzień 11
  • 2023 Tydzień 10

Najnowsze komentarze

  • Lukas L - Podsumowanie roku 2022
  • Oktawian Kurzynski - Enumeracja – wartość domyślna
  • Mateusz Stanek - Typy generyczne
  • Ola - Enumeracja – wartość domyślna
  • Typy generyczne w języku C# nie są 'rozwiązywane' w czasie kompilacji - Typy generyczne

Archiwa

  • marzec 2025
  • marzec 2023
  • luty 2023
  • styczeń 2023
  • grudzień 2022
  • marzec 2018
  • luty 2018
  • kwiecień 2017
  • marzec 2017
  • luty 2017

Kategorie

  • Bez kategorii
  • DSP2017
  • Elektronika
  • MiroBurn Challange
  • Narzędzia
  • Podsumowanie
  • Produktywność
  • Programowanie
  • Projekt
  • Prywatne
  • Rok 2023
  • Tips

Ostatnie wpisy

  • Rola Prelegent
  • Elektronika – powrót do starego hobby
  • Badger2040
  • 2023 Tydzień 11
  • 2023 Tydzień 10
Dumnie wspierane przez WordPressa | Motyw: Neblue by NEThemes.
Ta strona korzysta z ciasteczek aby świadczyć usługi na najwyższym poziomie. Dalsze korzystanie ze strony oznacza, że zgadzasz się na ich użycie.Zgoda
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT