0
私は同様の問題のために見つけた他の解決策を私が知っていると思っています。複製する。リスト内のオブジェクトのプロパティを別の(public)クラスに戻す
私はかなり新しい#で、Windowsフォームで単純な取引ゲームを構築しています。
要するに、dgv内のセルにそのオブジェクトの名前が含まれているセルをクリックすると、リストオブジェクトのDescriptionプロパティが返されます。リストは途中で別のパブリッククラスにあります。ここで
は、私がこれまで持っているものです。
public class Commodity
{
public int ID { get; set; }
public int Cost { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public bool IsPerishable { get; set; }
public bool IsIllegal { get; set; }
public Commodity (int id, int cost, string name, string description, bool isPerishable, bool isIllegal)
{
ID = id;
Cost = cost;
Name = name;
Description = description;
IsPerishable = isPerishable;
IsIllegal = isIllegal;
}
}
...
public static class World
{
public static readonly List<Commodity> Commodities = new List<Commodity>();
...
public const int COMMODITY_ID_TEA = 1;
public const int COMMODITY_ID_SPICES = 2;
public const int COMMODITY_ID_FISH = 3;
public const int COMMODITY_ID_SILK = 4;
public const int COMMODITY_ID_SLAVES = 5;
public const int COMMODITY_ID_NARCOTICS = 6;
public const int COMMODITY_ID_FRUIT = 7;
public const int COMMODITY_ID_MEAT = 8;
public const int COMMODITY_ID_BOOKS = 9;
public const int COMMODITY_ID_METAL= 10;
public const int COMMODITY_ID_COAL = 11;
public const int COMMODITY_ID_WEAPONS = 12;
public const int COMMODITY_ID_SUGAR_CANE= 13;
public const int COMMODITY_ID_MEDICINE = 14;
public const int COMMODITY_ID_DEEDS = 15;
...
static World()
{
PopulateCommodities()
}
...
private static void PopulateCommodities()
{
Commodities.Add(new Commodity(COMMODITY_ID_TEA, 25, "Tea", "Dried leaves that get boiled then drunk.", false, false));
Commodities.Add(new Commodity(COMMODITY_ID_SPICES, 50, "Spices", "Individually wrapped though, not a secret blend of 13.", false, false));
Commodities.Add(new Commodity(COMMODITY_ID_FISH, 35, "Fish", "Smells like fish.", true, false));
Commodities.Add(new Commodity(COMMODITY_ID_SILK, 120, "Silk", "Luxurious material that makes really comfy pants.", false, false));
Commodities.Add(new Commodity(COMMODITY_ID_SLAVES, 800, "Slaves", "Who doesn't want their own slave? Careful though, it's against the law to trade these in some places.", false, true));
Commodities.Add(new Commodity(COMMODITY_ID_NARCOTICS, 2000, "Narcotics", "Substances that are dangerous and illegal - don't get caught buying or selling!", false, true));
Commodities.Add(new Commodity(COMMODITY_ID_FRUIT, 400, "Fruit", "Exotic fruits from all over the world. Good in smoothies.", true, false));
Commodities.Add(new Commodity(COMMODITY_ID_MEAT, 750, "Meat", "Cured meat. Cured from what, though?", false, false));
Commodities.Add(new Commodity(COMMODITY_ID_BOOKS, 1250, "Books", "First edition collectibles. Don't get them wet.", false, false));
Commodities.Add(new Commodity(COMMODITY_ID_METAL, 800, "Metal", "Construction materials. For building with.", false, false));
Commodities.Add(new Commodity(COMMODITY_ID_COAL, 1000, "Coal", "Black rocks that burn really well.", false, false));
Commodities.Add(new Commodity(COMMODITY_ID_WEAPONS, 5000, "Weapons", "Guns and bombs and stuff. Illegal in some places.", false, true));
Commodities.Add(new Commodity(COMMODITY_ID_SUGAR_CANE, 1500, "Sugar Cane", "Sweet!", true, false));
Commodities.Add(new Commodity(COMMODITY_ID_MEDICINE, 7500, "Medicine", "Maybe these cured the meat?", false, false));
Commodities.Add(new Commodity(COMMODITY_ID_DEEDS, 15000, "Deeds", "Title deeds to some prime land!", false, false));
}
...
public static Commodity CommodityByID(int id)
{
foreach(Commodity commodity in Commodities)
{
if(commodity.ID == id)
{
return commodity;
}
}
return null;
}
...ので、情報が構築されてきたかのthats。
public partial class Plunder : Form
{
... // The dgv is populated at this point.
private void dgvShopStock_CellClick(object sender, DataGridViewCellEventArgs e)
{
string chosenCell = dgvShopStock.SelectedCells[0].Value.ToString();
string chosenType = dgvShopStock.Columns[0].Name.ToString();
//So far so good - chosenCell returns the contents and chosenType returns the column header as there are other types of object which can appear in this box - not important now.
//Here's me trying IndexOf. Returns a 'cannot convert string to Commodity' error. I tried casting without success.
int index = World.Commodities.IndexOf(chosenCell);
//This attempts to construct the strings "World.CommodityByID" and the parameter for it of "World.COMMODITY_ID_BOOKS" in this case. This was to attempt reflection instead but I dont fully understand it and don't know where to go from here.
string methodName = "World." + chosenType + "ByID";
string param = "World." + chosenType.ToUpper() + "_ID_" + chosenCell.ToUpper();
Commodity returnedItem;
MethodInfo chosenItem = this.GetType().GetMethod(methodName);
chosenItem.Invoke(this, param);
rtbDescription.Text = returnedItem.Description;
}
}
この長さのため申し訳ありませんが、部外者の目から私の状況を説明するのが得意I'mnotので、私は、関連するコードが含まれるように最善のことを考えました。私は間違った質問をして、しかしここにあったとして
おかげ