Tại sao tôi không thể thấy phương pháp mở rộng enum này? (Tôi nghĩ tôi điên mất).Tại sao tôi không thể "thấy" phương pháp mở rộng enum này?
File1.cs
namespace Ns1
{
public enum Website : int
{
Website1 = 0,
Website2
}
}
File2.cs
using Ns1;
namespace Ns2
{
public class MyType : RequestHandler<Request, Response>
{
public override Response Handle(Request request, CRequest cRequest)
{
//does not compile, cannot "see" ToDictionary
var websites = Website.ToDictionary<int>();
return null;
}
}
//converts enum to dictionary of values
public static class EnumExtensions
{
public static IDictionary ToDictionary<TEnumValueType>(this Enum e)
{
if(typeof(TEnumValueType).FullName != Enum.GetUnderlyingType(e.GetType()).FullName) throw new ArgumentException("Invalid type specified.");
return Enum.GetValues(e.GetType())
.Cast<object>()
.ToDictionary(key => Enum.GetName(e.GetType(), key),
value => (TEnumValueType) value);
}
}
}
nên nó không phải là trang web WebSite = WebSite.Website1; là một enum? – btlog
@btlog: Cả hai đều hợp lệ. Trong trường hợp này, anh ta không sử dụng giá trị thực tế để nó không có sự khác biệt. –
@btlog: 'new Website()' cho kết quả tương tự như 'Website.Website1' vì nó trả về giá trị mặc định, giá trị mặc định là 0. Không phải tôi khuyên bạn nên sử dụng cú pháp này :-) –