Click or drag to resize

ExtensionsConvertionsAs Method (Object, Type, Type)

Converts a type using the implicit or explicit operators. If both fails it will try to convert the value with ChangeType(Object, Type).

Namespace:  Cauldron
Assembly:  Cauldron (in Cauldron.dll) Version: 3.2.0.2
Syntax
public static Object As(
	this Object source,
	Type sourceType,
	Type targetType
)

Parameters

source
Type: SystemObject
The object to convert
sourceType
Type: SystemType
The type of the object to convert
targetType
Type: SystemType
The type to convert to

Return Value

Type: Object
A new instance of targetType.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type Object. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).
Examples
public class Bla
{
    public string Value { get; set; }

    public static implicit operator Bla(string value) => new Bla { Value = value };

    public static implicit operator Bla(int value) => new Bla { Value = value.ToString() };
}
The code can be called like following:
var bar = "Test Test".As(typeof(string), typeof(Bla));
See Also