System.NotSupportedException – třída

Tento článek obsahuje doplňující poznámky k referenční dokumentaci pro toto rozhraní API.

NotSupportedException značí, že pro vyvolanou metodu nebo vlastnost neexistuje žádná implementace.

NotSupportedException používá HODNOTU HRESULT COR_E_NOTSUPPORTED, která má hodnotu 0x80131515.

Seznam počátečních hodnot vlastností pro instanci NotSupportedExceptionnaleznete v NotSupportedException konstruktorech.

Vyvolání výjimky NotSupportedException

V následujících případech můžete zvážit vyvolání NotSupportedException výjimky:

  • Implementujete rozhraní pro obecné účely a počet metod nemá smysluplnou implementaci. Pokud například vytváříte typ data a času, který implementuje IConvertible rozhraní, vyvoláte NotSupportedException výjimku pro většinu převodů.

  • Zdědili jste z abstraktní třídy, která vyžaduje, abyste přepsali řadu metod. Jste ale připraveni poskytnout implementaci pouze pro podmnožinu těchto. Pro metody, které se rozhodnete neimplementovat, můžete zvolit vyvolání NotSupportedException.

  • Definujete typ pro obecné účely se stavem, který umožňuje podmíněně provádět operace. Váš typ může být například jen pro čtení nebo pro čtení i zápis. V takovém případě:

    • Pokud je objekt jen pro čtení, pokus o přiřazení hodnot k vlastnostem instance nebo volání metod, které upravují stav instance, by měla vyvolat NotSupportedException výjimku.

    • Měli byste implementovat vlastnost, která vrací Boolean hodnotu, která označuje, zda je konkrétní funkce k dispozici. Například u typu, který může být jen pro čtení nebo pro čtení i zápis, můžete implementovat IsReadOnly vlastnost, která označuje, zda je sada metod pro čtení i zápis k dispozici nebo nedostupná.

Zpracování výjimky NotSupportedException

Výjimka NotSupportedException označuje, že metoda nemá žádnou implementaci a že byste ji neměli volat. Tuto výjimku byste neměli zpracovat. Místo toho, co byste měli udělat, závisí na příčině výjimky: zda implementace zcela chybí, nebo volání člena je nekonzistentní s účelem objektu (například volání FileStream.Write metody na objektu jen FileStream pro čtení).

Implementace nebyla zadána, protože operaci nelze provést smysluplným způsobem. Jedná se o běžnou výjimku při volání metod na objektu, který poskytuje implementace pro metody abstraktní základní třídy, nebo implementuje rozhraní pro obecné účely a metoda nemá smysluplnou implementaci.

Třída například Convert implementuje IConvertible rozhraní, což znamená, že musí obsahovat metodu pro převod každého primitivního typu na každý druhý primitivní typ. Mnoho z těchto převodů však není možné. V důsledku toho volání Convert.ToBoolean(DateTime) metody například vyvolá NotSupportedException výjimku, protože neexistuje možný převod mezi DateTime hodnotou a hodnotou Boolean .

Chcete-li odstranit výjimku, měli byste eliminovat volání metody.

Volání metody není podporováno vzhledem ke stavu objektu. Pokoušíte se vyvolat člena, jehož funkce není k dispozici kvůli stavu objektu. Výjimku můžete odstranit jedním ze tří způsobů:

  • Stav objektu znáte předem, ale vyvolali jste nepodporovanou metodu nebo vlastnost. V tomto případě je vyvolání člena chybou a můžete ji odstranit.

  • Stav objektu znáte předem (obvykle proto, že kód vytvořil instanci), ale objekt je nesprávně nakonfigurovaný. Tento problém ilustruje následující příklad. Vytvoří objekt jen pro FileStream čtení a pak se do něj pokusí zapisovat.

    using System;
    using System.IO;
    using System.Text;
    using System.Threading.Tasks;
    
    public class Example
    {
        public static async Task Main()
        {
            Encoding enc = Encoding.Unicode;
            String value = "This is a string to persist.";
            Byte[] bytes = enc.GetBytes(value);
    
            FileStream fs = new FileStream(@".\TestFile.dat",
                                           FileMode.Open,
                                           FileAccess.Read);
            Task t = fs.WriteAsync(enc.GetPreamble(), 0, enc.GetPreamble().Length);
            Task t2 = t.ContinueWith((a) => fs.WriteAsync(bytes, 0, bytes.Length));
            await t2;
            fs.Close();
        }
    }
    // The example displays the following output:
    //    Unhandled Exception: System.NotSupportedException: Stream does not support writing.
    //       at System.IO.Stream.BeginWriteInternal(Byte[] buffer, Int32 offset, Int32 count, AsyncCallback callback, Object state
    //    , Boolean serializeAsynchronously)
    //       at System.IO.FileStream.BeginWrite(Byte[] array, Int32 offset, Int32 numBytes, AsyncCallback userCallback, Object sta
    //    teObject)
    //       at System.IO.Stream.<>c.<BeginEndWriteAsync>b__53_0(Stream stream, ReadWriteParameters args, AsyncCallback callback,
    //    Object state)
    //       at System.Threading.Tasks.TaskFactory`1.FromAsyncTrim[TInstance,TArgs](TInstance thisRef, TArgs args, Func`5 beginMet
    //    hod, Func`3 endMethod)
    //       at System.IO.Stream.BeginEndWriteAsync(Byte[] buffer, Int32 offset, Int32 count)
    //       at System.IO.FileStream.WriteAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken)
    //       at System.IO.Stream.WriteAsync(Byte[] buffer, Int32 offset, Int32 count)
    //       at Example.Main()
    
    open System.IO
    open System.Text
    
    let main = task {
        let enc = Encoding.Unicode
        let value = "This is a string to persist."
        let bytes  = enc.GetBytes value
    
        let fs = new FileStream(@".\TestFile.dat", FileMode.Open, FileAccess.Read)
        let t = fs.WriteAsync(enc.GetPreamble(), 0, enc.GetPreamble().Length)
        let t2 = t.ContinueWith(fun a -> fs.WriteAsync(bytes, 0, bytes.Length))
        let! _ = t2
        fs.Close()
    }
    main.Wait()
    
    // The example displays the following output:
    //    Unhandled Exception: System.NotSupportedException: Stream does not support writing.
    //       at System.IO.Stream.BeginWriteInternal(Byte[] buffer, Int32 offset, Int32 count, AsyncCallback callback, Object state
    //    , Boolean serializeAsynchronously)
    //       at System.IO.FileStream.BeginWrite(Byte[] array, Int32 offset, Int32 numBytes, AsyncCallback userCallback, Object sta
    //    teObject)
    //       at System.IO.Stream.<>c.<BeginEndWriteAsync>b__53_0(Stream stream, ReadWriteParameters args, AsyncCallback callback,
    //    Object state)
    //       at System.Threading.Tasks.TaskFactory`1.FromAsyncTrim[TInstance,TArgs](TInstance thisRef, TArgs args, Func`5 beginMet
    //    hod, Func`3 endMethod)
    //       at System.IO.Stream.BeginEndWriteAsync(Byte[] buffer, Int32 offset, Int32 count)
    //       at System.IO.FileStream.WriteAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken)
    //       at System.IO.Stream.WriteAsync(Byte[] buffer, Int32 offset, Int32 count)
    //       at <StartupCode:fs>.main()
    
    Imports System.IO
    Imports System.Text
    Imports System.Threading.Tasks
    
    Module Example
       Public Sub Main()
          Dim enc As Encoding = Encoding.Unicode
          Dim value As String = "This is a string to persist."
          Dim bytes() As Byte = enc.GetBytes(value)
    
          Dim fs As New FileStream(".\TestFile.dat", 
                                   FileMode.Open,
                                   FileAccess.Read)
          Dim t As Task = fs.WriteAsync(enc.GetPreamble(), 0, enc.GetPreamble().Length)
          Dim t2 As Task = t.ContinueWith(Sub(a) fs.WriteAsync(bytes, 0, bytes.Length)) 
          t2.Wait()
          fs.Close()
       End Sub
    End Module
    ' The example displays the following output:
    '    Unhandled Exception: System.NotSupportedException: Stream does not support writing.
    '       at System.IO.Stream.BeginWriteInternal(Byte[] buffer, Int32 offset, Int32 count, AsyncCallback callback, Object state
    '    , Boolean serializeAsynchronously)
    '       at System.IO.FileStream.BeginWrite(Byte[] array, Int32 offset, Int32 numBytes, AsyncCallback userCallback, Object sta
    '    teObject)
    '       at System.IO.Stream.<>c.<BeginEndWriteAsync>b__53_0(Stream stream, ReadWriteParameters args, AsyncCallback callback,
    '    Object state)
    '       at System.Threading.Tasks.TaskFactory`1.FromAsyncTrim[TInstance,TArgs](TInstance thisRef, TArgs args, Func`5 beginMet
    '    hod, Func`3 endMethod)
    '       at System.IO.Stream.BeginEndWriteAsync(Byte[] buffer, Int32 offset, Int32 count)
    '       at System.IO.FileStream.WriteAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken)
    '       at System.IO.Stream.WriteAsync(Byte[] buffer, Int32 offset, Int32 count)
    '       at Example.Main()
    

    Ycan eliminovat výjimku tím, že zajistí, že instance objekt podporuje funkce, které máte v úmyslu. Následující příklad řeší problém objektu jen FileStream pro čtení poskytnutím správných argumentů konstruktoru FileStream.FileStream(String, FileMode, FileAccess) .

  • Předem neznáte stav objektu a objekt nepodporuje konkrétní operaci. Ve většině případů by objekt měl obsahovat vlastnost nebo metodu, která označuje, zda podporuje konkrétní sadu operací. Výjimku můžete odstranit tak, že zkontrolujete hodnotu objektu a vyvoláte člena pouze v případě potřeby.

    Následující příklad definuje metodu DetectEncoding , která vyvolá NotSupportedException výjimku při pokusu o čtení od začátku datového proudu, který nepodporuje přístup pro čtení.

    using System;
    using System.IO;
    using System.Threading.Tasks;
    
    public class TestPropEx1
    {
        public static async Task Main()
        {
            String name = @".\TestFile.dat";
            var fs = new FileStream(name,
                                    FileMode.Create,
                                    FileAccess.Write);
            Console.WriteLine("Filename: {0}, Encoding: {1}",
                              name, await FileUtilities1.GetEncodingType(fs));
        }
    }
    
    public class FileUtilities1
    {
        public enum EncodingType
        { None = 0, Unknown = -1, Utf8 = 1, Utf16 = 2, Utf32 = 3 }
    
        public async static Task<EncodingType> GetEncodingType(FileStream fs)
        {
            Byte[] bytes = new Byte[4];
            int bytesRead = await fs.ReadAsync(bytes, 0, 4);
            if (bytesRead < 2)
                return EncodingType.None;
    
            if (bytesRead >= 3 & (bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF))
                return EncodingType.Utf8;
    
            if (bytesRead == 4)
            {
                var value = BitConverter.ToUInt32(bytes, 0);
                if (value == 0x0000FEFF | value == 0xFEFF0000)
                    return EncodingType.Utf32;
            }
    
            var value16 = BitConverter.ToUInt16(bytes, 0);
            if (value16 == (ushort)0xFEFF | value16 == (ushort)0xFFFE)
                return EncodingType.Utf16;
    
            return EncodingType.Unknown;
        }
    }
    // The example displays the following output:
    //    Unhandled Exception: System.NotSupportedException: Stream does not support reading.
    //       at System.IO.FileStream.BeginRead(Byte[] array, Int32 offset, Int32 numBytes, AsyncCallback callback, Object state)
    //       at System.IO.Stream.<>c.<BeginEndReadAsync>b__46_0(Stream stream, ReadWriteParameters args, AsyncCallback callback, Object state)
    //       at System.Threading.Tasks.TaskFactory`1.FromAsyncTrim[TInstance, TArgs](TInstance thisRef, TArgs args, Func`5 beginMethod, Func`3 endMethod)
    //       at System.IO.Stream.BeginEndReadAsync(Byte[] buffer, Int32 offset, Int32 count)
    //       at System.IO.FileStream.ReadAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken)
    //       at System.IO.Stream.ReadAsync(Byte[] buffer, Int32 offset, Int32 count)
    //       at FileUtilities.GetEncodingType(FileStream fs) in C:\Work\docs\program.cs:line 26
    //       at Example.Main() in C:\Work\docs\program.cs:line 13
    //       at Example.<Main>()
    
    open System
    open System.IO
    
    module FileUtilities =
        type EncodingType =
            | None = 0
            | Unknown = -1
            | Utf8 = 1
            | Utf16 = 2
            | Utf32 = 3
    
        let getEncodingType (fs: FileStream) = 
            task {
                let bytes = Array.zeroCreate<byte> 4
                let! bytesRead = fs.ReadAsync(bytes, 0, 4)
                if bytesRead < 2 then
                    return EncodingType.None
    
                elif bytesRead >= 3 && bytes[0] = 0xEFuy && bytes[1] = 0xBBuy && bytes[2] = 0xBFuy then
                    return EncodingType.Utf8
                else
                    let value = BitConverter.ToUInt32(bytes, 0)
                    if bytesRead = 4 && (value = 0x0000FEFFu || value = 0xFEFF0000u) then
                        return EncodingType.Utf32
                    else
                        let value16 = BitConverter.ToUInt16(bytes, 0)
                        if value16 = 0xFEFFus || value16 = 0xFFFEus then
                            return EncodingType.Utf16
                        else
                            return EncodingType.Unknown
            }
    
    let main _ = 
        task {
            let name = @".\TestFile.dat"
            let fs = new FileStream(name, FileMode.Create, FileAccess.Write)
            let! et = FileUtilities.getEncodingType fs
            printfn $"Filename: {name}, Encoding: {et}"
        }
    
    // The example displays the following output:
    //    Unhandled Exception: System.NotSupportedException: Stream does not support reading.
    //       at System.IO.FileStream.BeginRead(Byte[] array, Int32 offset, Int32 numBytes, AsyncCallback callback, Object state)
    //       at System.IO.Stream.<>c.<BeginEndReadAsync>b__46_0(Stream stream, ReadWriteParameters args, AsyncCallback callback, Object state)
    //       at System.Threading.Tasks.TaskFactory`1.FromAsyncTrim[TInstance, TArgs](TInstance thisRef, TArgs args, Func`5 beginMethod, Func`3 endMethod)
    //       at System.IO.Stream.BeginEndReadAsync(Byte[] buffer, Int32 offset, Int32 count)
    //       at System.IO.FileStream.ReadAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken)
    //       at System.IO.Stream.ReadAsync(Byte[] buffer, Int32 offset, Int32 count)
    //       at FileUtilities.GetEncodingType(FileStream fs)
    //       at Example.Main()
    //       at Example.<Main>()
    
    Imports System.IO
    Imports System.Threading.Tasks
    
    Module Example2
        Public Sub Main()
            Dim name As String = ".\TestFile.dat"
            Dim fs As New FileStream(name,
                                   FileMode.Create,
                                   FileAccess.Write)
            Console.WriteLine("Filename: {0}, Encoding: {1}",
                            name, FileUtilities2.GetEncodingType(fs))
        End Sub
    End Module
    
    Public Class FileUtilities2
        Public Enum EncodingType As Integer
            None = 0
            Unknown = -1
            Utf8 = 1
            Utf16 = 2
            Utf32 = 3
        End Enum
    
        Public Shared Function GetEncodingType(fs As FileStream) As EncodingType
            Dim bytes(3) As Byte
            Dim t As Task(Of Integer) = fs.ReadAsync(bytes, 0, 4)
            t.Wait()
            Dim bytesRead As Integer = t.Result
            If bytesRead < 2 Then Return EncodingType.None
    
            If bytesRead >= 3 And (bytes(0) = &HEF AndAlso bytes(1) = &HBB AndAlso bytes(2) = &HBF) Then
                Return EncodingType.Utf8
            End If
    
            If bytesRead = 4 Then
                Dim value As UInteger = BitConverter.ToUInt32(bytes, 0)
                If value = &HFEFF Or value = &HFEFF0000 Then
                    Return EncodingType.Utf32
                End If
            End If
    
            Dim value16 As UInt16 = BitConverter.ToUInt16(bytes, 0)
            If value16 = &HFEFF Or value16 = &HFFFE Then
                Return EncodingType.Utf16
            End If
    
            Return EncodingType.Unknown
        End Function
    End Class
    ' The example displays the following output:
    '    Unhandled Exception: System.NotSupportedException: Stream does not support reading.
    '       at System.IO.Stream.BeginReadInternal(Byte[] buffer, Int32 offset, Int32 count, AsyncCallback callback, Object state,
    '     Boolean serializeAsynchronously)
    '       at System.IO.FileStream.BeginRead(Byte[] array, Int32 offset, Int32 numBytes, AsyncCallback userCallback, Object stat
    '    eObject)
    '       at System.IO.Stream.<>c.<BeginEndReadAsync>b__43_0(Stream stream, ReadWriteParameters args, AsyncCallback callback, O
    '    bject state)
    '       at System.Threading.Tasks.TaskFactory`1.FromAsyncTrim[TInstance,TArgs](TInstance thisRef, TArgs args, Func`5 beginMet
    '    hod, Func`3 endMethod)
    '       at System.IO.Stream.BeginEndReadAsync(Byte[] buffer, Int32 offset, Int32 count)
    '       at System.IO.FileStream.ReadAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken)
    '       at System.IO.Stream.ReadAsync(Byte[] buffer, Int32 offset, Int32 count)
    '       at FileUtilities2.GetEncodingType(FileStream fs)
    '       at Example.Main()
    

    Výjimku můžete odstranit prozkoumáním hodnoty FileStream.CanRead vlastnosti a ukončením metody, pokud je stream jen pro čtení.

        public static async Task<EncodingType> GetEncodingType(FileStream fs)
        {
            if (!fs.CanRead)
                return EncodingType.Unknown;
    
            Byte[] bytes = new Byte[4];
            int bytesRead = await fs.ReadAsync(bytes, 0, 4);
            if (bytesRead < 2)
                return EncodingType.None;
    
            if (bytesRead >= 3 & (bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF))
                return EncodingType.Utf8;
    
            if (bytesRead == 4)
            {
                var value = BitConverter.ToUInt32(bytes, 0);
                if (value == 0x0000FEFF | value == 0xFEFF0000)
                    return EncodingType.Utf32;
            }
    
            var value16 = BitConverter.ToUInt16(bytes, 0);
            if (value16 == (ushort)0xFEFF | value16 == (ushort)0xFFFE)
                return EncodingType.Utf16;
    
            return EncodingType.Unknown;
        }
    }
    // The example displays the following output:
    //       Filename: .\TestFile.dat, Encoding: Unknown
    
    let getEncodingType (fs: FileStream) = 
        task {
            if not fs.CanRead then
                return EncodingType.Unknown
            else
                let bytes = Array.zeroCreate<byte> 4
                let! bytesRead = fs.ReadAsync(bytes, 0, 4)
                if bytesRead < 2 then
                    return EncodingType.None
    
                elif bytesRead >= 3 && bytes[0] = 0xEFuy && bytes[1] = 0xBBuy && bytes[2] = 0xBFuy then
                    return EncodingType.Utf8
                else
                    let value = BitConverter.ToUInt32(bytes, 0)
                    if bytesRead = 4 && (value = 0x0000FEFFu || value = 0xFEFF0000u) then
                        return EncodingType.Utf32
                    else
                        let value16 = BitConverter.ToUInt16(bytes, 0)
                        if value16 = 0xFEFFus || value16 = 0xFFFEus then
                            return EncodingType.Utf16
                        else
                            return EncodingType.Unknown
        }
    // The example displays the following output:
    //       Filename: .\TestFile.dat, Encoding: Unknown
    
    Public Class FileUtilities3
        Public Enum EncodingType As Integer
            None = 0
            Unknown = -1
            Utf8 = 1
            Utf16 = 2
            Utf32 = 3
        End Enum
    
        Public Shared Function GetEncodingType(fs As FileStream) As EncodingType
            If Not fs.CanRead Then
                Return EncodingType.Unknown
            End If
    
            Dim bytes(3) As Byte
            Dim t As Task(Of Integer) = fs.ReadAsync(bytes, 0, 4)
            t.Wait()
            Dim bytesRead As Integer = t.Result
            If bytesRead < 2 Then Return EncodingType.None
    
            If bytesRead >= 3 And (bytes(0) = &HEF AndAlso bytes(1) = &HBB AndAlso bytes(2) = &HBF) Then
                Return EncodingType.Utf8
            End If
    
            If bytesRead = 4 Then
                Dim value As UInteger = BitConverter.ToUInt32(bytes, 0)
                If value = &HFEFF Or value = &HFEFF0000 Then
                    Return EncodingType.Utf32
                End If
            End If
    
            Dim value16 As UInt16 = BitConverter.ToUInt16(bytes, 0)
            If value16 = &HFEFF Or value16 = &HFFFE Then
                Return EncodingType.Utf16
            End If
    
            Return EncodingType.Unknown
        End Function
    End Class
    ' The example displays the following output:
    '       Filename: .\TestFile.dat, Encoding: Unknown
    

Výjimka NotSupportedException úzce souvisí se dvěma dalšími typy výjimek;

  • NotImplementedException

    Tato výjimka se vyvolá, pokud je možné implementovat metodu, ale není, protože člen bude implementován v novější verzi, člen není k dispozici na konkrétní platformě, nebo člen patří do abstraktní třídy a odvozená třída musí poskytnout implementaci.

  • InvalidOperationException

    Tato výjimka je vyvolána ve scénářích, ve kterých je obecně možné, aby objekt provedl požadovanou operaci a stav objektu určuje, zda lze operaci provést.

Poznámky k rozhraní .NET Compact Framework

Při práci s rozhraním .NET Compact Framework a použitím volání nespravovaného kódu v nativní funkci může dojít k této výjimce, pokud:

  • Deklarace ve spravovaném kódu je nesprávná.
  • Rozhraní .NET Compact Framework nepodporuje to, co se pokoušíte udělat.
  • Názvy knihoven DLL jsou při exportu mangovány.

Pokud dojde k výjimce NotSupportedException , zkontrolujte:

  • Pro případná porušení omezení volání nespravovaného kódu rozhraní .NET Compact Framework.
  • Pro všechny argumenty, které vyžadují předem přidělenou paměť. Pokud existují, měli byste předat odkaz na existující proměnnou.
  • Názvy exportovaných funkcí jsou správné. To je možné ověřit pomocí DumpBin.exe.
  • Že se nepokoušejte předat příliš mnoho argumentů.