This project has moved and is read-only. For the latest updates, please go here.

System.Type Extension Methods

Type.CreateInstance Method

A Type extension method that creates an instance of the type.

Syntax

object CreateInstance(Type type,object[] values)

Parameters

type
The type to act on.
values
A variable-length parameters list containing arguments.

Examples


 // Copyright (c) 2013 Jonathan Magnan (http://zzzportal.com)
 // All rights reserved.
 // Licensed under MIT License (MIT)
 // License can be found here: https://zextensionmethods.codeplex.com/license
 
 using System;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 using Z;
 
 namespace ExtensionMethods.Examples
 {
     [TestClass]
     public class System_Type_CreateInstance
     {
         [TestMethod]
         public void CreateInstance()
         {
             // Type
             Type @this = typeof (object);
 
             // Examples
             object value = @this.CreateInstance(); // return new object();
 
             // Unit Test
             Assert.IsNotNull(value);
         }
     }
 }
                

Type.CreateInstance Method

A Type extension method that creates an instance of the type.

Syntax

T CreateInstance(Type type,object[] values)

Parameters

type
The type to act on.
values
A variable-length parameters list containing arguments.

Type Parameters

T
Generic type parameter.

Examples


 // Copyright (c) 2013 Jonathan Magnan (http://zzzportal.com)
 // All rights reserved.
 // Licensed under MIT License (MIT)
 // License can be found here: https://zextensionmethods.codeplex.com/license
 
 using System;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 using Z;
 
 namespace ExtensionMethods.Examples
 {
     [TestClass]
     public class System_Type_CreateInstance
     {
         [TestMethod]
         public void CreateInstance()
         {
             // Type
             Type @this = typeof (object);
 
             // Examples
             object value = @this.CreateInstance(); // return new object();
 
             // Unit Test
             Assert.IsNotNull(value);
         }
     }
 }
                

Type.GetPrivateField Method

A Type extension method that gets a private field.

Syntax

FieldInfo GetPrivateField(Type @this,string name)

Parameters

@this
The @this to act on.
name
The name.

Examples


 // Copyright (c) 2013 Jonathan Magnan (http://zzzportal.com)
 // All rights reserved.
 // Licensed under MIT License (MIT)
 // License can be found here: https://zextensionmethods.codeplex.com/license
 
 using System;
 using System.Reflection;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 using Z;
 
 namespace ExtensionMethods.Examples
 {
     [TestClass]
     public class System_Type_GetPrivateField
     {
         [TestMethod]
         public void GetPrivateField()
         {
             // Type
             Type @this = typeof (TestClass);
 
             // Examples
             FieldInfo value = @this.GetPrivateField("PrivateField"); // return FieldInfo.
 
             // Unit Test
             Assert.IsNotNull(value);
         }
 
         public class TestClass
         {
             private int PrivateField;
             public int PublicField;
             public int PublicProperty { get; set; }
             private int PrivateProperty { get; set; }
         }
     }
 }
                

Type.GetPrivateProperty Method

A Type extension method that gets private property.

Syntax

PropertyInfo GetPrivateProperty(Type @this,string name)

Parameters

@this
The @this to act on.
name
The name.

Examples


 // Copyright (c) 2013 Jonathan Magnan (http://zzzportal.com)
 // All rights reserved.
 // Licensed under MIT License (MIT)
 // License can be found here: https://zextensionmethods.codeplex.com/license
 
 using System;
 using System.Reflection;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 using Z;
 
 namespace ExtensionMethods.Examples
 {
     [TestClass]
     public class System_Type_GetPrivateProperty
     {
         [TestMethod]
         public void GetPrivateProperty()
         {
             // Type
             Type @this = typeof (TestClass);
 
             // Examples
             PropertyInfo value = @this.GetPrivateProperty("PrivateProperty"); // return PropertyInfo.
 
             // Unit Test
             Assert.IsNotNull(value);
         }
 
         public class TestClass
         {
             private int PrivateField;
             public int PublicField;
             public int PublicProperty { get; set; }
             private int PrivateProperty { get; set; }
         }
     }
 }
                

Type.GetPublicField Method

A Type extension method that gets a public field.

Syntax

FieldInfo GetPublicField(Type @this,string name)

Parameters

@this
The @this to act on.
name
The name.

Examples


 // Copyright (c) 2013 Jonathan Magnan (http://zzzportal.com)
 // All rights reserved.
 // Licensed under MIT License (MIT)
 // License can be found here: https://zextensionmethods.codeplex.com/license
 
 using System;
 using System.Reflection;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 using Z;
 
 namespace ExtensionMethods.Examples
 {
     [TestClass]
     public class System_Type_GetPublicField
     {
         [TestMethod]
         public void GetPublicField()
         {
             // Type
             Type @this = typeof (TestClass);
 
             // Examples
             FieldInfo value = @this.GetPublicField("PublicField"); // return FieldInfo.
 
             // Unit Test
             Assert.IsNotNull(value);
         }
 
         public class TestClass
         {
             private int PrivateField;
             public int PublicField;
             public int PublicProperty { get; set; }
             private int PrivateProperty { get; set; }
         }
     }
 }
                

Type.GetPublicProperty Method

A Type extension method that gets public property.

Syntax

PropertyInfo GetPublicProperty(Type @this,string name)

Parameters

@this
The @this to act on.
name
The name.

Examples


 // Copyright (c) 2013 Jonathan Magnan (http://zzzportal.com)
 // All rights reserved.
 // Licensed under MIT License (MIT)
 // License can be found here: https://zextensionmethods.codeplex.com/license
 
 using System;
 using System.Reflection;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 using Z;
 
 namespace ExtensionMethods.Examples
 {
     [TestClass]
     public class System_Type_GetPublicProperty
     {
         [TestMethod]
         public void GetPublicProperty()
         {
             // Type
             Type @this = typeof (TestClass);
 
             // Examples
             PropertyInfo value = @this.GetPublicProperty("PublicProperty"); // return PropertyInfo.
 
             // Unit Test
             Assert.IsNotNull(value);
         }
 
         public class TestClass
         {
             private int PrivateField;
             public int PublicField;
             public int PublicProperty { get; set; }
             private int PrivateProperty { get; set; }
         }
     }
 }
                

Last edited Nov 23, 2013 at 7:56 PM by Jonathan_Magnan, version 1