Generics in C#

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ayufan

    Generics in C#

    Hello,

    I have been trying to write code with generics. But following syntax is bad,
    in my opinion it shouldn't be.

    --- CODE ---
    public unsafe void Test2(void* data)
    {
    ....
    }

    public unsafe void Test<T>(T value) where T : struct
    {
    Test2((void*)&v alue);
    }
    --- END CODE ---

    Compiler says that "Cannot take the address of, get the size of, or declare
    a pointer to a managed type ('T')"
    but T is value type...
    Is there any other solution's?
    Sorry about my english...

    Thanks.
  • Mattias Sjögren

    #2
    Re: Generics in C#

    [color=blue]
    >but T is value type...[/color]

    You can't use all value type in unsafe code. And since the compiler
    can't be sure you're only using types that are "compatible " with
    unsafe code, code like that is not allowed.

    [color=blue]
    >Is there any other solution's?[/color]

    Unfortunately not.



    Mattias

    --
    Mattias Sjögren [MVP] mattias @ mvps.org
    http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
    Please reply only to the newsgroup.

    Comment

    • Dodo Cibelli

      #3
      Re: Generics in C#


      I thick this is a solution

      GCHandle dataHandle = GCHandle.Alloc( Data, GCHandleType.Pi nned);
      public void* pData = (void*) dataHandle.Addr OfPinnedObject( );

      as in the following class.

      Unfortunatey you can't do this

      public S* pData;
      pData = ( S*) dataHandle.Addr OfPinnedObject( );

      Just the declare gives a compiling error
      Does anyone know how to cast a pointer to a generic type?


      this works

      unsafe public class PinnedClass<S> where S : new()
      {
      public S Data;
      public void* pData;
      private GCHandle dataHandle;

      public PinnedClass()
      {
      Data = new S();
      dataHandle = GCHandle.Alloc( Data, GCHandleType.Pi nned);
      pData = (void*) dataHandle.Addr OfPinnedObject( );
      }

      public void Pin()
      {
      UnPin();
      dataHandle = GCHandle.Alloc( Data, GCHandleType.Pi nned);
      pData = (void*)dataHand le.AddrOfPinned Object();
      }

      public void UnPin()
      {
      if (pData != null)
      {
      dataHandle.Free ();
      pData = null;
      }
      }


      }

      this is what I'd like but does not compile

      unsafe public class PinnedClass<S> where S : new()
      {
      public S Data;
      public S* pData;
      private GCHandle dataHandle;

      public PinnedClass()
      {
      Data = new S();
      dataHandle = GCHandle.Alloc( Data, GCHandleType.Pi nned);
      pData = (S*) dataHandle.Addr OfPinnedObject( );
      }

      public void Pin()
      {
      UnPin();
      dataHandle = GCHandle.Alloc( Data, GCHandleType.Pi nned);
      pData = (S*)dataHandle. AddrOfPinnedObj ect();
      }

      public void UnPin()
      {
      if (pData != null)
      {
      dataHandle.Free ();
      pData = null;
      }
      }


      }




      "Mattias Sjögren" wrote:
      [color=blue]
      >[color=green]
      > >but T is value type...[/color]
      >
      > You can't use all value type in unsafe code. And since the compiler
      > can't be sure you're only using types that are "compatible " with
      > unsafe code, code like that is not allowed.
      >
      >[color=green]
      > >Is there any other solution's?[/color]
      >
      > Unfortunately not.
      >
      >
      >
      > Mattias
      >
      > --
      > Mattias Sjögren [MVP] mattias @ mvps.org
      > http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
      > Please reply only to the newsgroup.
      >[/color]

      Comment

      Working...