0

I have a method that has a signature that looks like this:

public IList<T> GetReferenceData<T>(TransactionManager transactionManager = null) { IList<T> collection; var cacheData = DataCacheManager.Instance.GetCacheItem(typeof(T).Name); if (cacheData != null) { collection = (IList<T>)cacheData; } else { collection = this.GetReferenceDataNoCache<T>(transactionManager); DataCacheManager.Instance.AddCacheItem(typeof(T).Name, collection); } return collection; } 

I have another method that allows me to pass in a string, which converts that string to the appropriate type. I then want to call the above method.

public IList GetReferenceDataByType(string referenceType) { // this works and returns the appropriate type correctly var type = this.GetEntity(referenceType); // now I'm stuck return this.GetReferenceData<?>(); } 

What replaces the question mark?

4
  • 2
    If you need this method there's probably something going wrong. Commented Mar 5, 2014 at 11:52
  • 1
    @TimSchmelter This is not the issue. He already has the type. He want to obtains a fully resolved generic method based on variable type parameters Commented Mar 5, 2014 at 11:52
  • Could you post GetReferenceData<T> body? Commented Mar 5, 2014 at 12:01
  • @TimSchmelter request is coming from an SOA web service. It can't pass internal types. It is actually passing an enumeration. I just simplified the code to make it a string so that I didn't need to start posting enums as well. Commented Mar 5, 2014 at 12:28

3 Answers 3

1

If I understand your question correctly you want somethiong like this:

public IList GetReferenceDataByType(string referenceType) { // this works and returns the appropriate type correctly var type = this.GetEntity(referenceType); var method = this.GetType().GetMethod("GetReferenceData"); var generic = method.MakeGenericMethod(type); return (IList) generic.Invoke(this, new object[] { null }); } 

Note that IList<T> does not implement IList so that cast may fail.

Sign up to request clarification or add additional context in comments.

1 Comment

Might be that the generic invoke cant detect the default parameter, try my updated answer.
0

Looks like you should invert your approach.
Instead of calling generic methods from non-generic alternatives, your GetReferenceData and GetReferenceDataNoCache should be rewritten as non-generic ones:

public IList GetReferenceData(Type type, TransactionManager transactionManager = null) { // ... } private IList GetReferenceDataNoCache(Type type, TransactionManager transactionManager = null) { // ... } public IList<T> GetReferenceData<T>(TransactionManager transactionManager = null) { return (IList<T>)GetReferenceData(typeof(T), transactionManager); } 

Look at your code: the only benefit from T in GetReferenceData<T> is typeof(T).
The rest of method is non-generic in fact.

4 Comments

While you have a good point, I have over 50 underlying reference tables to deal with. The generics save a fair amount of code.
How the quantity of tables is related to method structure?
Sorry I misunderstood your answer. I thought you were implying that I would need 50+ separate GetReferenceData(TypeX typeX... declarations.
@Junto: of course, no. I mean, that you're trying to call generic method from non-generic one. This makes sense, if the generic method is really does some generic stuff. But your GetReferenceData, in fact, use its type parameter only to retrieve the Type instance. The rest of method doesn't depend on type parameter and can be rewritten to be non-generic.
0

Visit these websites

http://predicatet.blogspot.com/2009/04/c-string-to-generic-type-conversion.html!

Universal generic type conversion from string

Convert string to generic type (basic or array) in C#

i hope it will help you

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.