How To

How to Get all classes that implement interface in C#
By Tan Lee Published on Feb 04, 2025 671

You can use reflection to retrieve all classes in the current assembly that implement a specific interface.

How to Use Convert.ChangeType to convert string to any type in C#
By Tan Lee Published on Feb 04, 2025 1.06K

You can utilize Convert.ChangeType() to transform a string into any type, like this:

How to Convert a string to an int in C#
By Tan Lee Published on Feb 04, 2025 364

When working with C#, you'll often need to convert a string into an integer.

How to convert char to int in C#
By Tan Lee Published on Feb 04, 2025 443

onverting a char to an int means obtaining the numeric value that the char represents (e.g., ‘1’ becomes 1). This is different from casting a char to an int, which gives you the char's ASCII or Unicode value (e.g., ‘1’ is 49 in Unicode).

How to get types from assembly in C#
By Tan Lee Published on Feb 04, 2025 777

In C#, you can extract type information from an assembly through a reflection-only load. This process allows you to examine metadata without executing the assembly, which can help avoid runtime errors that typically occur with a fully loaded assembly.

How to Convert a list of strings into a set of enums in C#
By Tan Lee Published on Feb 04, 2025 406

Imagine you have a list of user roles that you read from a configuration file or a database when your application starts. Whenever a user logs in, you want to check if their assigned role is part of the allowed roles list.

How to Get subclass properties with reflection in C#
By Tan Lee Published on Feb 04, 2025 526

When you use reflection to get properties, you can retrieve only the subclass's properties by using BindingFlags.DeclaredOnly (which excludes inherited properties).

How to Check if a property is an enum with reflection in C#
By Tan Lee Published on Feb 04, 2025 506

When working with reflection to inspect a class’s properties, you can use PropertyInfo.PropertyType.IsEnum to determine if the property is an enum type.

How to load assemblies at runtime using Microsoft Extensibility Framework in C#
By Tan Lee Published on Feb 04, 2025 417

The Microsoft Extensibility Framework (MEF) is a powerful tool that allows you to load assemblies dynamically at runtime.

Improving Performance by Reusing JsonSerializerOptions in C#
By Tan Lee Published on Feb 04, 2025 524

When dealing with object serialization in C#, the System.Text.Json library is often used for converting objects to JSON and vice versa.

How to detach entities in Entity Framework Core
By Tan Lee Published on Feb 03, 2025 401

Entity Framework (EF) Core is an open-source, object-relational mapper (ORM) that simplifies data access in your application.

How to Implement IP Whitelists in ASP.NET Core
By Tan Lee Published on Feb 03, 2025 399

When working with applications in ASP.NET Core, there may be scenarios where you want to restrict access to your API or web resources to only a specific set of trusted IP addresses.