Passing generic class to amethod that returns a generic ObservableCollection of the same class that is passed
up vote
0
down vote
favorite
I am working with pulling data from a DB in WPF and doing it with multiple different tables, which I have modeled as POCO's in C#. I want to be able to create a method that is flexible enough to handle any of these POCO's being passed in, and then return an observable collection of the same class to the caller.
I have something set up that I haven't tested yet, but I know this probably isn't the best way to implement this so I wanted to get some advice on how to best do this before I even bothered troubleshooting or attempting to get this to work:
public static ObservableCollection<object> SQLAuthentication(ObservableCollection<object> myCollection, object myClass, String sql)
{
var conn = new SqlConnection();
var paramList = GenerateSQLParameters(myClass, null);
var tempModel = Global.GenerateNewInstance(myClass);
//get the type
Type model = tempModel.GetType();
var prop = model.GetProperties();
PropertyInfo pi;
using (getConnection(conn))
{
conn.Open();
SqlCommand cmd;
SqlDataReader reader;
cmd = new SqlCommand(sql, conn);
reader = cmd.ExecuteReader();
while (reader.Read())
{
//set the values for each property in the model
foreach (var p in prop)
{
pi = tempModel.GetType().GetProperty(p.Name);
pi.SetValue(tempModel, reader[p.Name]);
}
myCollection.Add(tempModel);
}
reader.Close();
cmd.Dispose();
}
return myCollection;
}
c# wpf generics observablecollection
add a comment |
up vote
0
down vote
favorite
I am working with pulling data from a DB in WPF and doing it with multiple different tables, which I have modeled as POCO's in C#. I want to be able to create a method that is flexible enough to handle any of these POCO's being passed in, and then return an observable collection of the same class to the caller.
I have something set up that I haven't tested yet, but I know this probably isn't the best way to implement this so I wanted to get some advice on how to best do this before I even bothered troubleshooting or attempting to get this to work:
public static ObservableCollection<object> SQLAuthentication(ObservableCollection<object> myCollection, object myClass, String sql)
{
var conn = new SqlConnection();
var paramList = GenerateSQLParameters(myClass, null);
var tempModel = Global.GenerateNewInstance(myClass);
//get the type
Type model = tempModel.GetType();
var prop = model.GetProperties();
PropertyInfo pi;
using (getConnection(conn))
{
conn.Open();
SqlCommand cmd;
SqlDataReader reader;
cmd = new SqlCommand(sql, conn);
reader = cmd.ExecuteReader();
while (reader.Read())
{
//set the values for each property in the model
foreach (var p in prop)
{
pi = tempModel.GetType().GetProperty(p.Name);
pi.SetValue(tempModel, reader[p.Name]);
}
myCollection.Add(tempModel);
}
reader.Close();
cmd.Dispose();
}
return myCollection;
}
c# wpf generics observablecollection
That sounds like a XY problem: meta.stackexchange.com/questions/66377/what-is-the-xy-problem
– Christopher
Nov 10 at 21:46
Any reason why you're not just using Entity framework instead, or any other ORM
– Aydin Adn
Nov 10 at 21:56
1
You could declare it aspublic static ObservableCollection<T> SQLAuthentication(ObservableCollection<T> myCollection, T myClass, String sql) where T: class.
– John B. Lambe
Nov 10 at 21:59
@Aydin Adn yes I would love to but unfortunately its a corporate project and we have no access to nuget.
– MattE
Nov 10 at 23:14
@Christopher I'm not sure what you mean. I want to be able to query any table in the DB generically and return the values for it. Is there something else I should be asking? Not trying to be a smart ass, just trying to understand what you are getting at
– MattE
Nov 11 at 0:00
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am working with pulling data from a DB in WPF and doing it with multiple different tables, which I have modeled as POCO's in C#. I want to be able to create a method that is flexible enough to handle any of these POCO's being passed in, and then return an observable collection of the same class to the caller.
I have something set up that I haven't tested yet, but I know this probably isn't the best way to implement this so I wanted to get some advice on how to best do this before I even bothered troubleshooting or attempting to get this to work:
public static ObservableCollection<object> SQLAuthentication(ObservableCollection<object> myCollection, object myClass, String sql)
{
var conn = new SqlConnection();
var paramList = GenerateSQLParameters(myClass, null);
var tempModel = Global.GenerateNewInstance(myClass);
//get the type
Type model = tempModel.GetType();
var prop = model.GetProperties();
PropertyInfo pi;
using (getConnection(conn))
{
conn.Open();
SqlCommand cmd;
SqlDataReader reader;
cmd = new SqlCommand(sql, conn);
reader = cmd.ExecuteReader();
while (reader.Read())
{
//set the values for each property in the model
foreach (var p in prop)
{
pi = tempModel.GetType().GetProperty(p.Name);
pi.SetValue(tempModel, reader[p.Name]);
}
myCollection.Add(tempModel);
}
reader.Close();
cmd.Dispose();
}
return myCollection;
}
c# wpf generics observablecollection
I am working with pulling data from a DB in WPF and doing it with multiple different tables, which I have modeled as POCO's in C#. I want to be able to create a method that is flexible enough to handle any of these POCO's being passed in, and then return an observable collection of the same class to the caller.
I have something set up that I haven't tested yet, but I know this probably isn't the best way to implement this so I wanted to get some advice on how to best do this before I even bothered troubleshooting or attempting to get this to work:
public static ObservableCollection<object> SQLAuthentication(ObservableCollection<object> myCollection, object myClass, String sql)
{
var conn = new SqlConnection();
var paramList = GenerateSQLParameters(myClass, null);
var tempModel = Global.GenerateNewInstance(myClass);
//get the type
Type model = tempModel.GetType();
var prop = model.GetProperties();
PropertyInfo pi;
using (getConnection(conn))
{
conn.Open();
SqlCommand cmd;
SqlDataReader reader;
cmd = new SqlCommand(sql, conn);
reader = cmd.ExecuteReader();
while (reader.Read())
{
//set the values for each property in the model
foreach (var p in prop)
{
pi = tempModel.GetType().GetProperty(p.Name);
pi.SetValue(tempModel, reader[p.Name]);
}
myCollection.Add(tempModel);
}
reader.Close();
cmd.Dispose();
}
return myCollection;
}
c# wpf generics observablecollection
c# wpf generics observablecollection
asked Nov 10 at 21:34
MattE
390216
390216
That sounds like a XY problem: meta.stackexchange.com/questions/66377/what-is-the-xy-problem
– Christopher
Nov 10 at 21:46
Any reason why you're not just using Entity framework instead, or any other ORM
– Aydin Adn
Nov 10 at 21:56
1
You could declare it aspublic static ObservableCollection<T> SQLAuthentication(ObservableCollection<T> myCollection, T myClass, String sql) where T: class.
– John B. Lambe
Nov 10 at 21:59
@Aydin Adn yes I would love to but unfortunately its a corporate project and we have no access to nuget.
– MattE
Nov 10 at 23:14
@Christopher I'm not sure what you mean. I want to be able to query any table in the DB generically and return the values for it. Is there something else I should be asking? Not trying to be a smart ass, just trying to understand what you are getting at
– MattE
Nov 11 at 0:00
add a comment |
That sounds like a XY problem: meta.stackexchange.com/questions/66377/what-is-the-xy-problem
– Christopher
Nov 10 at 21:46
Any reason why you're not just using Entity framework instead, or any other ORM
– Aydin Adn
Nov 10 at 21:56
1
You could declare it aspublic static ObservableCollection<T> SQLAuthentication(ObservableCollection<T> myCollection, T myClass, String sql) where T: class.
– John B. Lambe
Nov 10 at 21:59
@Aydin Adn yes I would love to but unfortunately its a corporate project and we have no access to nuget.
– MattE
Nov 10 at 23:14
@Christopher I'm not sure what you mean. I want to be able to query any table in the DB generically and return the values for it. Is there something else I should be asking? Not trying to be a smart ass, just trying to understand what you are getting at
– MattE
Nov 11 at 0:00
That sounds like a XY problem: meta.stackexchange.com/questions/66377/what-is-the-xy-problem
– Christopher
Nov 10 at 21:46
That sounds like a XY problem: meta.stackexchange.com/questions/66377/what-is-the-xy-problem
– Christopher
Nov 10 at 21:46
Any reason why you're not just using Entity framework instead, or any other ORM
– Aydin Adn
Nov 10 at 21:56
Any reason why you're not just using Entity framework instead, or any other ORM
– Aydin Adn
Nov 10 at 21:56
1
1
You could declare it as
public static ObservableCollection<T> SQLAuthentication(ObservableCollection<T> myCollection, T myClass, String sql) where T: class .– John B. Lambe
Nov 10 at 21:59
You could declare it as
public static ObservableCollection<T> SQLAuthentication(ObservableCollection<T> myCollection, T myClass, String sql) where T: class .– John B. Lambe
Nov 10 at 21:59
@Aydin Adn yes I would love to but unfortunately its a corporate project and we have no access to nuget.
– MattE
Nov 10 at 23:14
@Aydin Adn yes I would love to but unfortunately its a corporate project and we have no access to nuget.
– MattE
Nov 10 at 23:14
@Christopher I'm not sure what you mean. I want to be able to query any table in the DB generically and return the values for it. Is there something else I should be asking? Not trying to be a smart ass, just trying to understand what you are getting at
– MattE
Nov 11 at 0:00
@Christopher I'm not sure what you mean. I want to be able to query any table in the DB generically and return the values for it. Is there something else I should be asking? Not trying to be a smart ass, just trying to understand what you are getting at
– MattE
Nov 11 at 0:00
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
You can use generic types in the method signature its self, you dont need to use object collection... and as im writing this John B hit the nail on its head
public static ObservableCollection<T> SQLAuthentication(ObservableCollection<T> myCollection, T myClass, String sql) where T: class
@John, if you post it as an answer ill remove this since you did technically shout it out first, just leave a comment if you do so i get the notification
Thanks! I had tried but forgot to add the T for myClass. Just getting back into the awing of things in C#...I've been in mostly javascript/angular the past year and a half...
– MattE
Nov 10 at 23:22
Actually this is almost correct but not fully. It throws errors until you also put the <T> on the method call itself also: public static ObservableCollection<T> SQLAuthentication<T>(ObservableCollection<T> myCollection, T myClass, String sql) where T: class is what worked
– MattE
Nov 10 at 23:55
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
You can use generic types in the method signature its self, you dont need to use object collection... and as im writing this John B hit the nail on its head
public static ObservableCollection<T> SQLAuthentication(ObservableCollection<T> myCollection, T myClass, String sql) where T: class
@John, if you post it as an answer ill remove this since you did technically shout it out first, just leave a comment if you do so i get the notification
Thanks! I had tried but forgot to add the T for myClass. Just getting back into the awing of things in C#...I've been in mostly javascript/angular the past year and a half...
– MattE
Nov 10 at 23:22
Actually this is almost correct but not fully. It throws errors until you also put the <T> on the method call itself also: public static ObservableCollection<T> SQLAuthentication<T>(ObservableCollection<T> myCollection, T myClass, String sql) where T: class is what worked
– MattE
Nov 10 at 23:55
add a comment |
up vote
1
down vote
accepted
You can use generic types in the method signature its self, you dont need to use object collection... and as im writing this John B hit the nail on its head
public static ObservableCollection<T> SQLAuthentication(ObservableCollection<T> myCollection, T myClass, String sql) where T: class
@John, if you post it as an answer ill remove this since you did technically shout it out first, just leave a comment if you do so i get the notification
Thanks! I had tried but forgot to add the T for myClass. Just getting back into the awing of things in C#...I've been in mostly javascript/angular the past year and a half...
– MattE
Nov 10 at 23:22
Actually this is almost correct but not fully. It throws errors until you also put the <T> on the method call itself also: public static ObservableCollection<T> SQLAuthentication<T>(ObservableCollection<T> myCollection, T myClass, String sql) where T: class is what worked
– MattE
Nov 10 at 23:55
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
You can use generic types in the method signature its self, you dont need to use object collection... and as im writing this John B hit the nail on its head
public static ObservableCollection<T> SQLAuthentication(ObservableCollection<T> myCollection, T myClass, String sql) where T: class
@John, if you post it as an answer ill remove this since you did technically shout it out first, just leave a comment if you do so i get the notification
You can use generic types in the method signature its self, you dont need to use object collection... and as im writing this John B hit the nail on its head
public static ObservableCollection<T> SQLAuthentication(ObservableCollection<T> myCollection, T myClass, String sql) where T: class
@John, if you post it as an answer ill remove this since you did technically shout it out first, just leave a comment if you do so i get the notification
answered Nov 10 at 21:59
Aydin Adn
9,45422036
9,45422036
Thanks! I had tried but forgot to add the T for myClass. Just getting back into the awing of things in C#...I've been in mostly javascript/angular the past year and a half...
– MattE
Nov 10 at 23:22
Actually this is almost correct but not fully. It throws errors until you also put the <T> on the method call itself also: public static ObservableCollection<T> SQLAuthentication<T>(ObservableCollection<T> myCollection, T myClass, String sql) where T: class is what worked
– MattE
Nov 10 at 23:55
add a comment |
Thanks! I had tried but forgot to add the T for myClass. Just getting back into the awing of things in C#...I've been in mostly javascript/angular the past year and a half...
– MattE
Nov 10 at 23:22
Actually this is almost correct but not fully. It throws errors until you also put the <T> on the method call itself also: public static ObservableCollection<T> SQLAuthentication<T>(ObservableCollection<T> myCollection, T myClass, String sql) where T: class is what worked
– MattE
Nov 10 at 23:55
Thanks! I had tried but forgot to add the T for myClass. Just getting back into the awing of things in C#...I've been in mostly javascript/angular the past year and a half...
– MattE
Nov 10 at 23:22
Thanks! I had tried but forgot to add the T for myClass. Just getting back into the awing of things in C#...I've been in mostly javascript/angular the past year and a half...
– MattE
Nov 10 at 23:22
Actually this is almost correct but not fully. It throws errors until you also put the <T> on the method call itself also: public static ObservableCollection<T> SQLAuthentication<T>(ObservableCollection<T> myCollection, T myClass, String sql) where T: class is what worked
– MattE
Nov 10 at 23:55
Actually this is almost correct but not fully. It throws errors until you also put the <T> on the method call itself also: public static ObservableCollection<T> SQLAuthentication<T>(ObservableCollection<T> myCollection, T myClass, String sql) where T: class is what worked
– MattE
Nov 10 at 23:55
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243633%2fpassing-generic-class-to-amethod-that-returns-a-generic-observablecollection-of%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
That sounds like a XY problem: meta.stackexchange.com/questions/66377/what-is-the-xy-problem
– Christopher
Nov 10 at 21:46
Any reason why you're not just using Entity framework instead, or any other ORM
– Aydin Adn
Nov 10 at 21:56
1
You could declare it as
public static ObservableCollection<T> SQLAuthentication(ObservableCollection<T> myCollection, T myClass, String sql) where T: class.– John B. Lambe
Nov 10 at 21:59
@Aydin Adn yes I would love to but unfortunately its a corporate project and we have no access to nuget.
– MattE
Nov 10 at 23:14
@Christopher I'm not sure what you mean. I want to be able to query any table in the DB generically and return the values for it. Is there something else I should be asking? Not trying to be a smart ass, just trying to understand what you are getting at
– MattE
Nov 11 at 0:00