Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8984749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:10:56+00:00 2026-06-15T21:10:56+00:00

I have this method public string DictionaryToString<T, U>(Dictionary<T, U> dict) { var valueStrings =

  • 0

I have this method

public string DictionaryToString<T, U>(Dictionary<T, U> dict)
{
  var valueStrings = dict.Select(x => x.Key.ToString() + ": " + x.Value.ToString());
  return String.Join("\n", valueStrings);
}

And I have this object that I want to pass into it

if ((value !=null) && value.GetType().IsGenericType &&
  value.GetType().GetGenericTypeDefinition() == typeof (Dictionary<,>))
{
   var castValue = value as Dictionary<,>; // this cast does not work
   return DictionaryToString(castValue);
}
else
{
  return value.ToString();
}

I can use reflection code like this in .Net 4.5

var targetMethodInfo = typeof(MyType).GetMethod("DictionaryToString");
var valueTypeArgs = value.GetType().GenericTypeArguments;
var genericMethod = targetMethodInfo.MakeGenericMethod(valueTypeArgs);
var result = genericMethod.Invoke(this, new[] {value });
return result.ToString();

ButType.GenericTypeArguments is new in .Net 4.5. So how can I do that cast in .Net 4.0?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T21:10:57+00:00Added an answer on June 15, 2026 at 9:10 pm

    You are only calling ToString on the key and value, so simply have this method take an IDictionary (non-generic), you are not using anything in there that is type-specific to T or U.

    You can then simply cast all arguments to IDictionary:

    var d = arg as IDictionary;
    
    if (d != null)
    {
        var res = DictionaryToString(d);
    }
    

    You may also need to amend the DictionaryToString implementation:

    static string DictionaryToString(IDictionary d)
    {
        var vals = new List<string>();
    
        foreach (DictionaryEntry de in d)
        {
            vals.Add(de.Key.ToString() + ": " + de.Value.ToString());
        }
    
        return String.Join("\n", vals);
    }
    

    Alternatively, if you really want to use LINQ, you could try casting to dynamic (it isn’t possible to cast to anything else as this could be a generic dictionary (KeyValuePair<>) or non-generic hashtable (DictionaryEntry)):

    var valueStrings = d.Cast<dynamic>().Select(de => de.Key.ToString() + ": " + de.Value.ToString());
    return string.Join("\n", valueStrings);
    

    This basically “duck types” the existence of the Key and Value properties.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this extension method public static string SerializeObject<T>(this T value) { var serializer
I have this method: public static Expression<Func<MyEntity, bool>> MyMethod(string someId) { return o =>
I have this toString method set up public String toString (){ String result; result
In code-behind of an ASP.NET page I have this method: public string TestFunc() {
I have this method: public void testJSNI2(){ String x = test; } I can
I have this method in cs page: public String getToolTip(Object productId, Object imgBtnId) {
I have this method call: public DataTemplate Create(Type type, string propertyName) { string str
I have this method to display the contact numbers in my inbox: public ArrayList<String>
I have a page with this method in CreateTicket.aspx.cs: [WebMethod()] public static string Categories()
I have a method that looks like this: public static String escape(String text) {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.