public interface IWeighted
{
public int Weight { get; set; }
}
public static class WeightedExtensions
{
public static KeyValuePair<T, IWeighted> GetWeightedRandom<T>(this IEnumerable<KeyValuePair<T, IWeighted>> weightList, System.Random randomInstance)
{
int max = 0;
foreach (var kvp in weightList)
{
kvp.Value.Weight += max;
}
int seleted = randomInstance.Next(1, max + 1);
foreach (var kvp in weightList)
{
if (seleted <= kvp.Value.Weight)
{
return kvp;
}
seleted -= kvp.Value.Weight;
}
return default;
}
}
이게 선언문
public Dictionary<ItemOption, OptionChunk> OptionChunks;
ItemOption 은 Enum, OptionChunk 는 Iweighted 잘 구현하고 있음
근데 Extension method 가 안보임
이거 왜 그런지 알려줄 수 있을까?
일단 namespace 문제는 아님
왜그런지는 deep하게는 모르겠고 좀 지저분하긴한데 public static KeyValuePair GetWeightedRandom(this IEnumerable> weightList, System.Random randomInstance) where U : IWeighted 이건 어때?
ㅇㅇ 제네릭 관련 문제 맞음 where 써서 비슷하게 해결했음