次の方法で共有


Convert.ChangeType メソッド (Object, Type, IFormatProvider)

指定した Type で、指定したオブジェクトに等しい値を持つ Object を返します。パラメータにより、カルチャに固有の書式情報が指定されます。

Overloads Public Shared Function ChangeType( _
   ByVal value As Object, _   ByVal conversionType As Type, _   ByVal provider As IFormatProvider _) As Object
[C#]
public static object ChangeType(objectvalue,TypeconversionType,IFormatProviderprovider);
[C++]
public: static Object* ChangeType(Object* value,Type* conversionType,IFormatProvider* provider);
[JScript]
public static function ChangeType(
   value : Object,conversionType : Type,provider : IFormatProvider) : Object;

パラメータ

  • value
    IConvertible インターフェイスを実装する Object
  • conversionType
    Type
  • provider
    カルチャに固有の書式情報を提供する IFormatProvider インターフェイス実装。

戻り値

TypeconversionType であり、value と等価の値を持つオブジェクト。

または

value および conversionType が null 参照 (Visual Basic では Nothing) の場合は null 参照 (Nothing) 。

または

value の TypeconversionType が等しい場合は value。

例外

例外の種類 条件
InvalidCastException value が IConvertible を実装しません。また、 conversionType が null 参照 (Visual Basic では Nothing) ではなく、value の Type と等価です。
ArgumentException conversionType が無効です。

解説

provider を使用すると、value の内容について、カルチャに固有の変換情報を指定できます。たとえば、 value が数値を表す String の場合、 provider によって、その数値の表記方法に関するカルチャに固有の情報を提供できます。

使用例

ChangeType の使用方法については、次のコード例を参照してください。

 
Imports System

Public Class ChangeTypeTest
    
    Public Shared Sub Main()
        Dim d As [Double] = - 2.345
        Dim i As Integer = CInt(Convert.ChangeType(d, GetType(Integer)))
        
        Console.WriteLine("The double value {0} when converted to an int becomes {1}", d, i)
        Dim s As String = "12/12/98"
        Dim dt As DateTime = CType(Convert.ChangeType(s, GetType(DateTime)), DateTime)
        
        Console.WriteLine("The string value {0} when converted to a Date becomes {1}", s, dt)
    End Sub 'Main
End Class 'ChangeTypeTest

[C#] 
using System;

public class ChangeTypeTest {
    public static void Main() {

        Double d = -2.345;
        int i = (int)Convert.ChangeType(d, typeof(int));

        Console.WriteLine("The double value {0} when converted to an int becomes {1}", d, i);

        string s = "12/12/98";
        DateTime dt = (DateTime)Convert.ChangeType(s, typeof(DateTime));

        Console.WriteLine("The string value {0} when converted to a Date becomes {1}", s, dt);        
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;

void main()
{

   Double d = -2.345;
   int i = *__try_cast<Int32*>(Convert::ChangeType(__box(d), __typeof(int)));

   Console::WriteLine(S"The double value {0} when converted to an int becomes {1}", __box(d), __box(i));

   String* s = S"12/12/98";
   DateTime dt = *__try_cast<DateTime*>(Convert::ChangeType(s, __typeof(DateTime)));

   Console::WriteLine(S"The String* value {0} when converted to a Date becomes {1}", s, __box(dt));        
}

[JScript] 
import System;

public class ChangeTypeTest {
    public static function Main() {

        var d : Double = -2.345;
        var i : Int32 = Int32(Convert.ChangeType(d, Int32));

        Console.WriteLine("The double value {0} when converted to an int becomes {1}", d, i);

        var s : String = "12/12/98";
        var dt : DateTime = DateTime(Convert.ChangeType(s, DateTime));

        Console.WriteLine("The string value {0} when converted to a Date becomes {1}", s, dt);        
    }
}

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

参照

Convert クラス | Convert メンバ | System 名前空間 | Convert.ChangeType オーバーロードの一覧