简体   繁体   中英

Unhandled exception. System.MissingMethodException: No parameterless constructor defined for type

I have a problem with this code but I don't have any clue what to do. Could you help me.

Error is:

Unhandled exception. System.MissingMethodException: No parameterless constructor defined for type 'Refleksija.Country'. at System.RuntimeType.CreateInstanceDefaultCtorSlow(Boolean publicOnly, Boolean wrapExceptions, Boolean fillCache) at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, Boolean wrapExceptions)
at System.Activator.CreateInstance(Type type, Boolean nonPublic, Boolean wrapExceptions) at System.Activator.CreateInstance(Type type) at Refleksija.Program.Main(String[] args) in C:\\Users\\darko.brakovic\\Source\\Repos\\Refleksija\\Refleksija\\Program.cs:line 14

Code is

class Program
{
    private static void Main(string[] args)
    {

        Assembly executingAssembly = Assembly.GetExecutingAssembly();
        Type countryType = executingAssembly.GetType("Refleksija.Country");
        object countryInstance = Activator.CreateInstance(countryType);

        MethodInfo getCountryInfoMethod = countryType.GetMethod("GetCountryInfo");

        string[] parametri = new string[1];
        parametri[0] = "Srbija";

        string CountryInfo = (string)getCountryInfoMethod.Invoke(countryInstance, parametri);

        Console.WriteLine("CountryInfo: = {0}", CountryInfo);

    }
}
class Country
{
    public string Name { get; set; }
    public int Population { get; set; }
    public Country(string name, int population)
    {
        Name = name;
        Population = population;
    }
    public string GetCountryInfo()
    {
        return "Country " + Name + " has the population of " + Population + ".";
    }
}

As pointed out in the comments, Activator.CreateInstance will only work for the case where you have a parameterless constructor (see the compiler error). For your example you can use GetConstructor like this:

class Program
{
    private static void Main(string[] args)
    {

        Assembly executingAssembly = Assembly.GetExecutingAssembly();
        Type countryType = executingAssembly.GetType("Refleksija.Country");
        var countryInstance =
            countryType.GetConstructor(new[] {typeof(string), typeof(int)})
                .Invoke(new object[] {"Srbija", 7_000_000});

        MethodInfo getCountryInfoMethod = countryType.GetMethod("GetCountryInfo");

        string CountryInfo = (string) getCountryInfoMethod.Invoke(countryInstance, new object[] { });

        Console.WriteLine("CountryInfo: = {0}", CountryInfo);

    }
}
class Country
{
    public string Name { get; set; }
    public int Population { get; set; }
    public Country(string name, int population)
    {
        Name = name;
        Population = population;
    }
    public string GetCountryInfo()
    {
        return "Country " + Name + " has the population of " + Population + ".";
    }
}

Also note that GetCountryInfo was being invoked with parameters, however it's a parameterless method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM