473,385 Members | 1,693 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,385 software developers and data experts.

Generic List Problem

Hi i have generic list problem with DAL code.
class UlazTotal

{


private System.DateTime uT_DATUMField;

private string rA_SIFRAField;

private decimal uT_IZNOSField;

..........

public System.DateTime UT_DATUM

{

get

{return this.uT_DATUMField;}

set

{this.uT_DATUMField = value;}

}
public string RA_SIFRA

{

get

{return this.rA_SIFRAField;}

set

{this.rA_SIFRAField = value;}

}
public decimal UT_IZNOS

{

get

{return this.uT_IZNOSField;}

set

{this.uT_IZNOSField = value;}

}.........................
}

I want to make generic method for getting all data from any class in solution.

public static List<TGetAll<T>(List<Tsource) where T: new()

{

List<Tlista = new List<T>();

SqlParameter[] parameters =

{};

SqlDataReader dr = SqlHelper.ExecuteReader(NoviTemplate.SqlHelper.Con nStr(), CommandType.StoredProcedure, "sp_UlazTotal_GetAll");

while (dr.Read())

{

T obj;

obj=new T();

Read(obj, dr); /////////////////HERE I GET ERROR

}

dr.Close();

return lista;

}

private static void Read(UlazTotal ulazTotal, SqlDataReader dr)

{

UlazTotal ulazTotal = new UlazTotal() ;

ulazTotal.UT_DATUM = Convert.ToDateTime(dr["UT_DATUM"].ToString());

ulazTotal.RA_SIFRA = dr["RA_SIFRA"].ToString();

ulazTotal.UT_IZNOS = Convert.ToDecimal(dr["UT_IZNOS"].ToString());

...............

}

How to make Generic Read method that accept any class (not ulazTotal class) and populate properties of any class with data from reader?

Thanks




Jul 16 '07 #1
1 1427
Well, you aren't going to be able to use the properties of the class
in this way, so I wouldn't even bother typing it... you are going to
have to use reflection or component-model access - something like:

void Read(object obj, IDataReader reader) {
PropertyDescriptorCollection props =
TypeDescriptor.GetProperties(obj);
for(int field = 0; field < reader.Fields; field++) {
PropertyDescriptor prop = props[reader.GetName(field)];
if(prop!=nulll) {
prop.SetValue(obj, reader[field]);
}
}
}

However - this is a very crude approach; perhaps look at ORMs like
NHibernate? Or LINQ in .NET 3.5? In these cases the object type comes
into play, so I might make it:

void Read<T>(T obj, IDataReader reader) {...}

Marc
Jul 16 '07 #2

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

Similar topics

5
by: majm | last post by:
I'm trying to implement strongly typed lists in the 2.0 framework. I'm using VS2005 beta 2. So far, System.Collections.Generic.List appears to be the ideal solution. However, the...
10
by: steve bull | last post by:
I have a class SwatchPanel which takes Swatch as a parameter type. How can I call a static function within the Swatch class? For example the code below fails on TSwatch.Exists. How can I get the...
2
by: Nicolas Fleury | last post by:
Hi, I have a field named "BaseClasses" in C++ as in the following simplified code: using Collections::Generic::List; namespace A2M { namespace LipIntrospection { public ref class...
3
by: Tigger | last post by:
I have an object which could be compared to a DataTable/List which I am trying to genericify. I've spent about a day so far in refactoring and in the process gone through some hoops and hit some...
0
by: crazyone | last post by:
I've got a gaming framework i'm building and i want to save myself the trouble of reading and writting the complete game data to a custom file and load/save it to an XML file but i'm getting...
3
by: Seth Gecko | last post by:
Hi I am working with generic lists of various objects and a control dealing with these lists. For instance: A parent form holds: dim Walls as List(Of wall) dim Segments as List(Of segment) ...
4
by: =?Utf-8?B?QkogU2FmZGll?= | last post by:
We have a class that has a public property that is of type List<T>. FXCop generates a DoNotExposeGenericLists error, indicating "System.Collections.Generic.List<Tis a generic collection designed...
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...
11
by: Scott Stark | last post by:
Hello, The code below represents a singly-linked list that accepts any type of object. You can see I'm represting the Data variable a System.Object. How would I update this code to use...
2
by: SimonDotException | last post by:
I am trying to use reflection in a property of a base type to inspect the properties of an instance of a type which is derived from that base type, when the properties can themselves be instances of...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.