Solved

Generic interface Return type

Posted on 2013-05-17
2
180 Views
Last Modified: 2014-03-28
Hi ,

I have a interface

public interface IMyInterface<T>
{
long Method(T obj);
}

My Base class is

public abstract class MyClass<T>:IMyInterface<T>
{
public abstract long Method(T Obj);
}

public class Class1
{
public string val1{get;set;}
public string val2{get;set;}
}

public class Class2
{
public string val3{get;set;}
public string val4{get;set;}

}

My implementation class would be

Public class MyderivedClass1:MyClass<Class1>
{
public override long Method(Class1 Obj)
{
             /// Method implementation
}
}

Public class MyderivedClass2:MyClass<Class2>
{
public override long Method(Class2 Obj)
{
             /// Method implementation
}
}


I have created a new method which would give the interface intance based on the input string

static IMyInterface<T> getintance<T>(string name)
{
if(name=="Class1")
{
IMyInterface<Class1> instance = new MyderivedClass1();
                    return (IMyInterface<T>)instance ;
}
else if (name=="Class1")
{
IMyInterface<Class2> instance = new MyderivedClass2();
                    return (IMyInterface<T>)instance ;
}

}


In my calling method i need to specify the particular class type to get the intance .

getintance<Class1>("Class1");
or
getintance<Class2>("Class2");

already i'm passing the string to identify the intance.

I need to get the intance without passing the class type.

Any help one this .
0
Comment
Question by:selva_kongu
2 Comments
 
LVL 10

Expert Comment

by:eguilherme
ID: 39174552
If you need the Interface anom to be typed you will need to pass the instance, otherwise the compiler wont know what it is, unless you work with dynamics but you lose intellisense with that
0
 
LVL 15

Accepted Solution

by:
Minh Võ Công earned 500 total points
ID: 39253432
static IMyInterface<T> getintance<T>()
{
string name = typeof(T);
if(name=="Class1")
{
IMyInterface<Class1> instance = new MyderivedClass1();
                    return (IMyInterface<T>)instance ;
}
else if (name=="Class1")
{
IMyInterface<Class2> instance = new MyderivedClass2();
                    return (IMyInterface<T>)instance ;
}

}

http://stackoverflow.com/questions/325156/calling-generic-method-with-a-type-argument-known-only-at-execution-time
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now