Tôi đã gặp sự cố với việc sử dụng các biến động trong C#. Điều này phát sinh khi mã hóa các mô-đun định tuyến NancyFx, nhưng tôi đã đun sôi vấn đề xuống ví dụ dưới đây. Trong khi tôi nhận được một ngoại lệ khác trong mã ban đầu, mã ví dụ vẫn ném một ngoại lệ mà tôi tin là sai. Có ai nhìn thấy những gì đang xảy ra ở đây, hoặc có những người khác gặp phải vấn đề tương tự?Lỗi có thể xảy ra trong C# động?
Lưu ý rằng bài viết sau đây có thể liên quan: StackOverflowException when accessing member of generic type via dynamic: .NET/C# framework bug? System.Dynamic bug?
Mã:
class Program
{
static void Main(string[] args)
{
var dictionary = new Dictionary<string, object>();
dictionary.Add("number", 12);
var result = MethodUsesExplicitDeclaration(dictionary);
var result2 = MethodUsesImplicitDeclaration(dictionary);
}
static dynamic MethodUsesExplicitDeclaration(dynamic reallyDictionary)
{
// this works, ostensibly because the local variable is explicitly declared
IDictionary<string, object> dictionary = CastDictionary(reallyDictionary);
return dictionary.Get<int>("number");
}
static dynamic MethodUsesImplicitDeclaration(dynamic reallyDictionary)
{
// this throws an exception, and the only difference is
// that the variable declaration is implicit
var dictionary = CastDictionary(reallyDictionary);
return dictionary.Get<int>("number");
}
static IDictionary<string, object> CastDictionary(dynamic arg)
{
return arg as IDictionary<string, object>;
}
}
static class Extensions
{
public static T Get<T>(this IDictionary<string, object> dictionary, string key)
{
var value = dictionary[key];
if (value is T)
return (T)value;
throw new InvalidOperationException();
}
}
Trường hợp ngoại lệ: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException was unhandled HResult=-2146233088 Message='System.Collections.Generic.Dictionary<string,object>' does not contain a definition for 'Get' Source=Anonymously Hosted DynamicMethods Assembly StackTrace: at CallSite.Target(Closure , CallSite , Object , String) at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1) at DynamicBug.Program.MethodUsesImplicitDeclaration(Object reallyDictionary) in c:\TFS\UnreleasedCode\POC\DynamicBug\DynamicBug\Program.cs:line 28 at DynamicBug.Program.Main(String[] args) in c:\TFS\UnreleasedCode\POC\DynamicBug\DynamicBug\Program.cs:line 16 InnerException:
Dường như câu hỏi này được trả lời [ở đây] (http://stackoverflow.com/a/258999/97382). –
Tôi không tin rằng http://stackoverflow.com/questions/258988/will-the-dynamic-keyword-in-c4-support-extension-methods giải quyết cùng một câu hỏi. Mã ví dụ không cố gắng sử dụng một phương thức mở rộng trên một biến được khai báo là động, nó sử dụng nó trên kết quả của một phương thức trả về một kiểu giao diện. Vì vậy, trong cả hai phiên bản của phương thức thực thi một phương thức mở rộng, kiểu mà phương thức mở rộng áp dụng không phải là động. –
Một cách khác tôi có thể hỏi câu hỏi ban đầu là khi nào một khai báo biến 'var' không hoạt động như kiểu giá trị mà bạn gán cho nó? –