Daily Archives: May 11, 2021

C# || How To Copy All Properties & Fields From One Object To Another Using C#

The following is a module with functions which demonstrates how to copy all properties and fields from one object to another using C#.

The function demonstrated on this page is a generic extension method which uses reflection to copy all the matching properties and fields from one object to another.

This function works on both structure and class object fields and properties.

The two objects do not have to be the same type. Only the matching properties and fields are copied.


1. Copy Properties & Fields

The example below demonstrates the use of ‘Utils.Objects.CopyPropsTo‘ to copy all the matching properties and fields from the source object to the destination object.


2. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


3. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

C# || Universal Object Serializer and Deserializer Using C#

The following is a module with functions which demonstrates how to create a simple universal object serializer and deserializer using C#.

The following functions take in a instance of the abstract classesObjectSerializer‘, and ‘ObjectDeserializer‘. Those classes defines the functionality that is common to all the classes derived from it. They are what carries out the actual serialization and deserialization.

Breaking things up like so makes it easy to have different serializations with only calling one function.

Note: The functions in this module uses code from previous articles which explains how to serialize and deserialize objects.

For those examples of how to serialize and deserialize objects using Json and XML, see below:


1. JsonSerializer

The example below demonstrates the use of ‘Utils.Objects.JsonSerializer‘ to serialize an object to Json.

By changing the second parameter, calling the same function will change its behavior.


2. JsonDeserializer

The example below demonstrates the use of ‘Utils.Objects.JsonDeserializer‘ to deserialize an object from Json.

By changing the second parameter, calling the same function will change its behavior.


3. XmlSerializer

The example below demonstrates the use of ‘Utils.Objects.XmlSerializer‘ to serialize an object to Xml.

By changing the second parameter, calling the same function will change its behavior.


4. XmlDeserializer

The example below demonstrates the use of ‘Utils.Objects.XmlDeserializer‘ to deserialize an object from xml.

By changing the second parameter, calling the same function will change its behavior.


5. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


6. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

C# || How To Send, Post & Process A REST API Web Request Using C#

The following is a module with functions which demonstrates how to send and receive a RESTful web request using C#.

Contents

1. Overview
2. WebRequest - GET
3. WebRequest - POST
4. WebRequest - PUT
5. WebRequest - PATCH
6. WebRequest - DELETE
7. Utils Namespace
8. More Examples


1. Overview

The following functions use System.Net.HttpWebRequest and System.Net.HttpWebResponse to send and process requests. They can be called synchronously or asynchronously. This page will demonstrate using the asynchronous function calls.

The examples on this page will call a test API, and the resulting calls will return Json results.

The Json objects we are sending to the API are hard coded in the examples below. In a real world application, the objects would be serialized first before they are sent over the network, and then deserialized once a response is received. For simplicity, those operations are hard coded.

For examples of how to serialize and deserialize objects using Json and XML, see below:

Note: Don’t forget to include the ‘Utils Namespace‘ before running the examples!


2. WebRequest – GET

The example below demonstrates the use of ‘Utils.WebRequest.Get‘ to execute a GET request on the given url.

The optional function parameter allows you to specify System.Net.HttpWebRequest options, like the UserAgent, Headers etc.


3. WebRequest – POST

The example below demonstrates the use of ‘Utils.WebRequest.Post‘ to execute a POST request on the given url.

The optional function parameter allows you to specify System.Net.HttpWebRequest options, like the UserAgent, Headers etc.


4. WebRequest – PUT

The example below demonstrates the use of ‘Utils.WebRequest.Put‘ to execute a PUT request on the given url.

The optional function parameter allows you to specify System.Net.HttpWebRequest options, like the UserAgent, Headers etc.


5. WebRequest – PATCH

The example below demonstrates the use of ‘Utils.WebRequest.Patch‘ to execute a PATCH request on the given url.

The optional function parameter allows you to specify System.Net.HttpWebRequest options, like the UserAgent, Headers etc.


6. WebRequest – DELETE

The example below demonstrates the use of ‘Utils.WebRequest.Delete‘ to execute a DELETE request on the given url.

The optional function parameter allows you to specify System.Net.HttpWebRequest options, like the UserAgent, Headers etc.


7. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


8. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

C# || How To Check If A String Is A Valid HTTP URL Using C#

The following is a module with functions which demonstrates how to check whether a string is a valid HTTP URL using C#.

Note: The following function only checks for valid url formatting. It does not determine if the address behind the url is valid for navigation.


1. Is Valid URL

The example below demonstrates the use of ‘Utils.Http.IsValidURL‘ to check whether a string is a valid HTTP URL.


2. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


3. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.