Pass Object from generic interface

Posted by Ogipansrk under C# on 2/2/2012 | Points: 10 | Views : 1598 | Status : [Member] | Replies : 3
public class factory
{
static public ITest<T> SomeMethod<T>(T someDatavalue)

{

// here c2 is a class, which has a method that returns customerslist

ITest<Customer> boo = new C2();

return (ITest<T>)boo;

}
}

Now from client how to use this object.
I want something similar like below

ITest<T> foo = Factory.SomeMethod(1);
foo.SomeMethod();




Responses

Posted by: Swapnil on: 2/2/2012 [Member] Starter | Points: 25

Up
0
Down
Can you please elaborate more?

As per my understanding, you are passing integer value (for replacing Generic type) and you want to get the list of customer (which is also same generic type integer.).

I have few queries.
1. What will be your input and what will be your output? What 1 represent in your input parameter? From your code it seems it is not passing anything to c2 class, so where it get used?
2. Why can't you directly make call to C2 class from your client?
3. What is the structure of ITest<T> interface?

Thanks,
Swapnil

Thanks and Regards,
Swapnil

Ogipansrk, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ogipansrk on: 2/2/2012 [Member] Starter | Points: 25

Up
0
Down
Good Questions,

That was an example.
My Requirement is I want to get accessed to a method eg: "GetCustomers" ,which is in some class namely "C2" ,
Which returns List<Customers>.
So I have created a generic Interface like below
Interface ITest<T>
{
List<T> GetCustomers();
}
Now I have Implemented this in C2 class.

Now from client I do not want to call C2 class directly .
For this reason , I want to create a another class namely 'FactoryClass".
public class factoryclass
{
static public ITest<T> SomeMethod<T>(T someDatavalue)

{

// here c2 is a class, which has a method that returns customerslist

ITest<Customer> boo = new C2();

return (ITest<T>)boo;

}
}

Now from client how to use this object.
I want something similar like below

ITest<T> foo = Factory.SomeMethod<object>(1); // here you guys need to help , how to code client
foo.SomeMethod(); //




Ogipansrk, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Swapnil on: 2/2/2012 [Member] Starter | Points: 25

Up
0
Down
Based on your explanation and my understanding, You can make following 2 change to make it work.

1.
// here c2 is a class, which has a method that returns customerslist

ITest<Customer> boo = new C2();
return (ITest<T>)boo;


to

// here c2 is a class, which has a method that returns customerslist

ITest<T> boo = (ITest<T>) new C2();
return boo;


2.
ITest<T> foo = Factory.SomeMethod<object>(1); // here you guys need to help , how to code client

foo.SomeMethod(); //


to

ITest<Customer> foo = factoryclass.SomeMethod<Customer>(new Customer());

foo.GetCustomers();


Thanks,
Swapnil

Thanks and Regards,
Swapnil

Ogipansrk, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response