logo
down
shadow

How to get input parameter Type using GetMethod?


How to get input parameter Type using GetMethod?

By : user2950142
Date : November 17 2020, 11:52 AM
wish help you to fix your issue You cannot use a runtime System.Type propertyType as a type parameter to a generic method. Instead, use the overload of DeserializeObject that takes a runtime type:
code :
argumentObject = JsonConvert.DeserializeObject(jsonStringObject, propertyType);


Share : facebook icon twitter icon
How to 'GetMethod' on multiple function definitions where match has generic parameter type?

How to 'GetMethod' on multiple function definitions where match has generic parameter type?


By : user3049727
Date : March 29 2020, 07:55 AM
hop of those help? There are two possible issues - finding a method if it is non-public (the example shows non-public), and handling the generics.
IMO, this is the easiest option here:
code :
MethodInfo generic = typeof(MyClass).GetMethods(
        BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
    .Single(x => x.Name == "GenerateOutput" && x.GetParameters().Length == 2)
    .MakeGenericMethod(type);
Passing the correct type parameter to MethodInfo GetMethod

Passing the correct type parameter to MethodInfo GetMethod


By : jAbrams
Date : March 29 2020, 07:55 AM
Any of those help Unfortunutely there is no easy way to get the correct overload when the parameter type is generic. You can do it using LINQ manually:
code :
typeof(Enumerable)
  .GetMethods(BindingFlags.Public | BindingFlags.Static)
  .First(x => x.Name == "FirstOrDefault" &&
              x.GetParameters().Length == 1 &&
              x.GetParameters()[0].ParameterType.GetGenericTypeDefinition() == typeof(IEnumerable<>));
Correct Parameter Type for GetMethod

Correct Parameter Type for GetMethod


By : user3721137
Date : March 29 2020, 07:55 AM
it should still fix some issue I believe the answer to your question is, "There is no way to do this".
Since GetMethod is not able to "MakeGenericMethod" while its doing its lookup, you have a method with one generic parameter that's known (TClass) and one that's not (TMethod). Reflection can't look up a method when some, but not all, of the parameters are known.
What Types should be in the Type array for Type.GetMethod when a parameter is generic?

What Types should be in the Type array for Type.GetMethod when a parameter is generic?


By : user3867022
Date : March 29 2020, 07:55 AM
seems to work fine I'll give you the full "thing" I use when I have to find a Queryable.Select Method (IQueryable, Expression>) method... It is quite complex and checks nearly all:
code :
MethodInfo selectMethod = (from x in typeof(Queryable).GetMethods(BindingFlags.Static | BindingFlags.Public)
                           where x.Name == "Select" && x.IsGenericMethod
                           let pars = x.GetParameters()
                           where pars.Length == 2
                           let args = x.GetGenericArguments()
                           where args.Length == 2 &&
                               pars[0].ParameterType == typeof(IQueryable<>).MakeGenericType(args[0]) &&
                               pars[1].ParameterType == typeof(Expression<>).MakeGenericType(typeof(Func<,>).MakeGenericType(args))
                           select x).Single();
MethodInfo method = (from x in typeof(Example).GetMethods(BindingFlags.Instance | BindingFlags.Public)
                     where x.Name == "DoSomething" && x.IsGenericMethod
                     let pars = x.GetParameters()
                     where pars.Length == 1
                     let args = x.GetGenericArguments()
                     where args.Length == 1 &&
                         pars[0].ParameterType == args[0]
                     select x).Single();
C# Type GetMethod() where parameter order is random

C# Type GetMethod() where parameter order is random


By : unhandled_exception
Date : March 29 2020, 07:55 AM
Hope that helps It is not sensible to implement a method that will ignore parameter order. Parameter order is critical to determining that you have found the correct method.
Consider this simple class:
code :
public class A 
{
    public void Foo(int a, string b)
    {
        PrintAString();
    }

    public void Foo(string b, int a)
    {
        FormatHardDrive();
    }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

public class Program
{
    public static void Main()
    {
        var typesToUse = new Type[] { typeof(int), typeof(string) };
        var methods = typeof(A).GetMethods().Where(m => m.Name == "Foo");

        var matchingMethods = methods.Where(m => ContainsAllParameters(m, typesToUse));

        Console.WriteLine(matchingMethods.Single());
    }

    private static bool ContainsAllParameters(MethodInfo method, Type[] typesToUse) 
    {
        var methodTypes = method.GetParameters().Select(p => p.ParameterType).ToList();

        foreach(var typeToUse in typesToUse)
        {
            if (methodTypes.Contains(typeToUse))
            {
                methodTypes.Remove(typeToUse);
            }
            else 
            {
                return false;       
            }
        }

        return !methodTypes.Any();
    }

}

public class A
{
    public void Foo(string a, int b) 
    {
        Console.WriteLine("Hello World");
    }
}
Related Posts Related Posts :
  • Why does C# also not allow empty conditions in while loops?
  • Unable to get a block of code into my regex match groups
  • What is difference between dbcontext.Add and dbcontext.AddObject
  • How do I update the file version number of C# DLL without recompiling?
  • Value for html control always null
  • HttpClient hangs when timeout is setting (Windows Phone)
  • Handle Multiple Form tag in asp.net page?
  • create word document with html content in c#
  • Confusion about Find And Replace
  • Format sms messages in Clickatell
  • Automated Function Overload
  • String to date in MS Access SQL statement gives type mismatch error
  • ShowDialog exiting on certain events
  • Is there a try Convert.ToInt32... avoiding exceptions
  • How to move wpf application into minimize tray at Window Start-up C#?
  • How to change display format of long variable?
  • How to use Addfields in MongoDB C# Aggregation Pipeline
  • MsTest TestCleanup method not called when an unhandled exception is thrown
  • missing last data when exporting gridview to excel
  • How to add array of objects to List in c#
  • Lambda Expression to order (sort) my list collection
  • Library for displaying music notation
  • How to compare two dictionaries in c# and get the output as True and False after validation
  • specify fields to be serialized with JSON
  • How do you obtain the content of a specific node using XmlDocument in C#?
  • How to ignore the first line in a csv file when you read the csv file in C#
  • c# - Problem calling public void from class
  • How to tell a class which objects it should create? Type vs. object confusion :(
  • Save CheckBox state to xml
  • WIX CAQuietExec NETSH Command Fails
  • Issues sending http put request every 60 seconds to RoR app
  • ConfigurationManager.ConnectionStrings.ConnectionString Issue
  • Real size WPF controls for printing
  • How to cancel properly?
  • C# String multiplication error
  • Using Solrnet and Assigning Attributes with Entity Framework Generated POCOs
  • Regex pattern for single backslash
  • TextBox: insert spaces for credit card number?
  • C# DLL loaded for exe-application is not found when launching similar DLL by rundll32.exe
  • Debug a Windows Service with WCF library
  • Open a file from an external assembly?
  • Servicestack RegistrationFeature Unable to bind request
  • Index was outside the bounds of the array confusion
  • Error in Xml to List code. The ':' character, hexadecimal value 0x3A, cannot be included in a name
  • I am trying to do a while loop with a string conditional statement in C#
  • C# 'Cannot access a disposed object. Object name: 'SslStream'.'
  • How to make Gecko use seperate CookieContainer per instance?
  • C# Advanced form "please wait"
  • Send and Receive data C# using network stream
  • How to discover that appsettings changed in C#?
  • Check what needs full trust
  • What is the execution order of an MVC Razor view/layout
  • Table designer (Entity Framework) is too resource intense
  • How to clean up an exception string so it can be displayed via Javascript Alert?
  • Configuring Amazon SES Feedback Notifications via Amazon SNS in ASP.NET MVC (C#)
  • C# api responce and request
  • Dynamodb putitem function not working properly
  • theme in windows phone(light or dark) using c#
  • Backup attached database file(.mdf) using c# and SQL Server
  • What is 'TextFile' and where it is uses in WPF project
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org