Pomoc c#

ninanina12

Početnik
Poruka
3
Molim pomoc oko sledeceg zadatka, stalno mi izbacuje gresku
Zadatak glasi:
Data je sledeća klasa:

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}.";
}

}


Unutar Main metode programa, korišćenjem refleksije obavite dinamičko kreiranje instance klase Country, a zatim i dinamičko pozivanje metode GetCountryInfo. Dobijeni rezultat prikažite unutar konzole.

ovako sam ja uradila tj. pokusala;-))






using System.Reflection;
using System.Text;


namespace System { }

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}.";
}

}



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

{
Assembly assembly = null;
try
{
assembly = Assembly.Load("Country");
}
catch (FileNotFoundException exc)
{

Console.WriteLine(exc.Message);
}
return;


Type country = assembly.GetType("Country");
object obj = Activator.CreateInstance(country);


MethodInfo methodInfo = country.GetMethod("GetCountryInfo");
object[] parameters = new object[] { };
methodInfo.Invoke(obj, parameters);

Console.ReadLine();
}

private static object CreateInstance(Type? country)
{
throw new NotImplementedException();
}
}

Hvala unapred na pomoci!
Nina
 
vrv bi pomoglo da ubacis kod u kod formater (mislim da ima ovde na krsti) da mogu da se lepo vide strukture i prati program.
vrv bi pomoglo takodje da napises koju zapravo gresku dobijas.

sto se samog zadatka tice u pitanju su ocigledno naprednija tehnike (a ja sam malo zahrdjao u C# - ne koristim ga 5g+) - ali ocigledno da se radi o dinamickim tehnikama u okiviru inace staticnog jezika. Do neke granice to i ide, ali od neke granice koncept jezika se opire.

Da je bmaxa tu on bi obicno to istresao iz rukava, ali ima ovde jos C# ljudi, naci ce se jos neko. Ja nemam ni gde bi eventualno mogao da ubacim program pa da ga probam.

Ono sto primecujem je da se main kod prerano zavrsava,
 
C#:
using System;
using System.Reflection;

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 }.";
  }

}

public class Program {
   
  static void Main( string[] args ) {
     
    Type country = typeof( Country );
    object obj = Activator.CreateInstance(
      country,
      new object[] {
        "Serbia",
        6834000
      }
    );

    MethodInfo methodInfo = country.GetMethod( "GetCountryInfo" );
    object[] parameters = new object[] {};
    Console.WriteLine( methodInfo.Invoke( obj, parameters ) );

    Console.ReadLine();
   
  }

}

Evo ti moje resenje. Jedan od problema je bio to sto nisi prosledila parametre "CreateInstance" metodi s obzirom da postoji samo jedan konstrutor "Country" klase koji ima dva parametra i to bez default-nih vrednosti.
 
Ovo je resenje ChatGPT-a.

C#:
using System;
using System.Reflection;

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}.";
    }
}

class Program
{
    static void Main(string[] args)
    {
        Assembly assembly = Assembly.GetExecutingAssembly();

        Type countryType = assembly.GetType("Country");

        // Parameters for the constructor
        object[] constructorArgs = { "Example Country", 1000000 };

        // Create an instance by passing arguments to the constructor
        object obj = Activator.CreateInstance(countryType, constructorArgs);

        MethodInfo methodInfo = countryType.GetMethod("GetCountryInfo");
        object[] parameters = new object[] { };
        string countryInfo = (string)methodInfo.Invoke(obj, parameters);

        Console.WriteLine(countryInfo);
        Console.ReadLine();
    }
}
 
C#:
using System;
using System.Reflection;

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 }.";
  }

}

public class Program {
  
  static void Main( string[] args ) {
    
    Type country = typeof( Country );
    object obj = Activator.CreateInstance(
      country,
      new object[] {
        "Serbia",
        6834000
      }
    );

    MethodInfo methodInfo = country.GetMethod( "GetCountryInfo" );
    object[] parameters = new object[] {};
    Console.WriteLine( methodInfo.Invoke( obj, parameters ) );

    Console.ReadLine();
  
  }

}

Evo ti moje resenje. Jedan od problema je bio to sto nisi prosledila parametre "CreateInstance" metodi s obzirom da postoji samo jedan konstrutor "Country" klase koji ima dva parametra i to bez default-nih vrednosti.
Puno vam hvala na pomoci ! Svaka vam cast da nadjete vremena za pomoc pocetnicima!
L.p
 
vrv bi pomoglo da ubacis kod u kod formater (mislim da ima ovde na krsti) da mogu da se lepo vide strukture i prati program.
vrv bi pomoglo takodje da napises koju zapravo gresku dobijas.

sto se samog zadatka tice u pitanju su ocigledno naprednija tehnike (a ja sam malo zahrdjao u C# - ne koristim ga 5g+) - ali ocigledno da se radi o dinamickim tehnikama u okiviru inace staticnog jezika. Do neke granice to i ide, ali od neke granice koncept jezika se opire.

Da je bmaxa tu on bi obicno to istresao iz rukava, ali ima ovde jos C# ljudi, naci ce se jos neko. Ja nemam ni gde bi eventualno mogao da ubacim program pa da ga probam.

Ono sto primecujem je da se main kod prerano zavrsava,
Hvala vam na podrsci!
 

Back
Top