How To

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

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 791

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 292

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 368

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 672

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 335

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 460

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 432

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 361

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 445

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 236

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 326

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.