Net Reflection Namespace

2023-05-05

System.Reflection Namespace ve Temel Sınıfları

Reflection, System.Reflection namespace’i içerisinde tanımlanmış bir dizi sınıf ve yapı ile gerçekleştirilir. Bu makalede, bu namespace’i ve temel sınıflarını inceleyerek, Reflection ile çalışmaya başlamak için ihtiyaç duyulan temel bilgileri sunacağız.

System.Reflection namespace’i, çalışma zamanında (runtime) derleme, modül, tür ve üye bilgilerine erişmeyi sağlayan sınıfları içerir. Başlıca sınıflar şunlardır:

  1. Assembly Assembly sınıfı, .NET uygulamalarındaki derlemeleri (assembly) temsil eder. Derleme, uygulamanın kodu ve metadatasını içeren bir bileşendir. Assembly sınıfı ile derleme yüklemesi, derleme bilgilerine erişim ve derleme içindeki türlerin listelenmesi gibi işlemler gerçekleştirilebilir.

Örnek kullanım:

Assembly assembly = Assembly.LoadFrom("MyAssembly.dll");
Type[] types = assembly.GetTypes();
  1. Type Type sınıfı, bir türün metadatasını temsil eder. Type nesneleri, çalışma zamanında tür bilgilerine erişmek ve türlerle ilgili işlemler yapmak için kullanılır.

Örnek kullanım:

Type myType = typeof(MyClass);
Console.WriteLine(myType.FullName);
  1. MethodInfo MethodInfo sınıfı, metotların metadatasını temsil eder. MethodInfo nesneleri, metotların özelliklerine erişmek, parametre bilgilerini almak ve çalışma zamanında metotları çağırmak için kullanılır.

Örnek kullanım:

MethodInfo methodInfo = myType.GetMethod("MyMethod");
object result = methodInfo.Invoke(myInstance, new object[] { "parameter1", 42 });
  1. PropertyInfo PropertyInfo sınıfı, özelliklerin (property) metadatasını temsil eder. PropertyInfo nesneleri, özellik bilgilerine erişmek ve çalışma zamanında özellik değerlerini okumak veya yazmak için kullanılır.

Örnek kullanım:

PropertyInfo propertyInfo = myType.GetProperty("MyProperty");
propertyInfo.SetValue(myInstance, "NewValue");
  1. FieldInfo FieldInfo sınıfı, alanların (field) metadatasını temsil eder. FieldInfo nesneleri, alan bilgilerine erişmek ve çalışma zamanında alan değerlerini okumak veya yazmak için kullanılır.

Örnek kullanım:

FieldInfo fieldInfo = myType.GetField("MyField");
fieldInfo.SetValue(myInstance, 123);


More posts like this

The SemaphoreSlim Class: Multitask-Based Programming in C#

2023-06-11 | #net #semaphoreslim

Overview The SemaphoreSlim class is a structure used in C# to control one or more threads using a specific resource or operation concurrently. SemaphoreSlim limits the number of threads that can access the resource at the same time. The use of SemaphoreSlim is often used to prevent deadlock situations in multi-threaded applications and to ensure that a specific resource is used by only one or more threads at the same time.

Continue reading 


LINQ and Optimistic Concurrency: Methods for Ensuring Data Integrity

2023-06-11 | #linq #net #optimistic-concurrency

When working with databases, it is of great importance to properly manage concurrent operations and ensure data integrity. Concurrency control is used to manage multiple users or processes accessing the same data concurrently. In this article, we will explore how to ensure data integrity using LINQ (Language Integrated Query) with optimistic concurrency. What is Optimistic Concurrency? Optimistic concurrency is a method where resources are not locked when a transaction begins, but changes are checked before the transaction is completed.

Continue reading 


LINQ and Pessimistic Concurrency: Methods for Ensuring Data Integrity

2023-06-11 | #linq #net #pessimistic-concurrency

When working with databases, managing concurrent operations and ensuring data integrity are of great importance. Concurrency control is used to manage multiple users or processes accessing the same data concurrently. In this article, we will explore how to use LINQ (Language Integrated Query) with pessimistic concurrency to maintain data integrity. What is Pessimistic Concurrency? Pessimistic concurrency is based on the principle of locking the relevant data when a transaction begins and maintaining the lock until the transaction is completed.

Continue reading 