473,378 Members | 1,410 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

C# 2.0 Generics: how to get address of data?

I'm using the 2.0 SDK and learning to use Generics. I'm trying to
obtain a pointer to a buffer (one of the members of a class) so that I
can pass it on to some unmanaged code. The code I have generates a
couple of compiler errors and I'm wondering how to work around them.
I've put the error messages below as comments just after the offending
lines. Can someone tell me how to accomplish what I'm after?
using System;

class Flub<T>
{
T[] data;

public Flub()
{
data = new T[4];
}

void ReadPixels(IntPtr buffer)
{
// call DLL
}

unsafe void Attempt1()
{
unsafe {
fixed(void* buffer = data) {
// ERROR ABOVE: cannot take the address of, get the size of, or
declare a pointer to a managed type ('T')
ReadPixels((IntPtr)buffer);
}
}
}

unsafe void Attempt2()
{
string typename = typeof(T).FullName;

if (typename == "System.Int32")
{
int[] ints = new int[data.Length];
unsafe {
fixed (int* ip = ints)
{
ReadPixels((IntPtr)ip);
for (int i = 0; i < ints.Length; i++)
data[i] = (T) ints[i];
// ERROR ABOVE: cannot convert type 'int' to 'T'
}
}
}
else
throw new SystemException();
}

}

The two error messages are pretty self explanatory. I'm surprised by
the second as I thought it would try to cast at runtime and throw an
exception if it couldn't.

The first message is a little unexpected as well. I know I can do
this:

public unsafe static void changeVal(int[] a)
{
fixed (int *b = a)
{
*b = 5;
*(b + 1) = 7;
}
}
.... assuming 'a' has already allocated space for two ints. But isn't
'a' a managed type too? Why can I get its address?
Nov 16 '05 #1
2 5304
Can someone tell me how to accomplish what I'm after?
Unfortunately you can't.

... assuming 'a' has already allocated space for two ints. But isn't
'a' a managed type too? Why can I get its address?


Because it's not known at compile time that T will be a "managed
type".

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #2
Surely there must be some way to get data from unmanaged code into a
generic data structure through a pointer. Specifically I picture
having a Raster generic that can have different underlying datatypes
(byte, ushort, uint). Can anyone tell me how I can do it? Or is it
really not possible?

Mattias Sjögren <ma********************@mvps.org> wrote in message news:<Oa*************@TK2MSFTNGP14.phx.gbl>...
Can someone tell me how to accomplish what I'm after?


Unfortunately you can't.

... assuming 'a' has already allocated space for two ints. But isn't
'a' a managed type too? Why can I get its address?


Because it's not known at compile time that T will be a "managed
type".

Mattias

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Andreas Huber | last post by:
What follows is a discussion of my experience with .NET generics & the ..NET framework (as implemented in the Visual Studio 2005 Beta 1), which leads to questions as to why certain things are the...
8
by: Ashish | last post by:
Hi all, I have interface declared like public IBaseInterface { } then a generic collection like
1
by: uttara | last post by:
I have a generic collection which I am using in classes to store a collection of embedded objects. Class Employee: IEntity { Private string mName; Private int mEmployeeID; …. Private...
3
by: Larry Charlton | last post by:
In .Net 2.0, how do I convert between arrays and generics? For example in my webservice if I define <WebMethod()> _ Public Function TakeList(ByVal list As List(Of Data)) As List(Of Data) Return...
3
by: LongBow | last post by:
Hello all, First of all, sorry for multiple question per one thread. I have two questions. First what I think might be the easier problem. I am capturing data from an embedded device which I...
8
by: mark.norgate | last post by:
I've run into a few problems trying to use generics for user controls (classes derived from UserControl). I'm using the Web Application model rather than the Web Site model. The first problem...
18
by: ttl_idiot | last post by:
This post is about how to get generic modules in a C environment. The aim is to get close to Ada generics. Using C++ is not an option. It should all run in a simple C compiler. When I think of a...
7
by: Ajeet | last post by:
hi I am having some difficulty in casting using generics. These are the classes. public interface IProvider<PROF> where PROF : IProviderProfile { //Some properties/methods }
13
by: rkausch | last post by:
Hello everyone, I'm writing because I'm frustrated with the implementation of C#'s generics, and need a workaround. I come from a Java background, and am currently writing a portion of an...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.