Re: Debug.Assert(dynamic)
От: Sharov Россия  
Дата: 20.05.19 11:52
Оценка: 36 (1)
Здравствуйте, snaphold, Вы писали:

S>Внешний сервис возвращает джейсон объект и надо проверить результат на наличие значения.

S>Как лучше?

S>Делаю так Debug.Assert(dynamic) — кидает ошибку Cannot dynamically invoke method 'Assert' because it has a Conditional attribute


The actual call in Main gets translated into a CallSite, because you're invoking the call with a dynamic param. The CallSite does all the preparation it needs in order to invoke this method at run-time. But, the problem is, that Assert has a Conditional attribute on it, which means it needs to pass information to the compiler pre-processor at compile-time

This blog post explains:

Conditional attributes can be placed on methods (and attributes in whidbey) to instruct the compiler to conditionally remove calls to the function if a symbol is not defined. This can be useful for debug-only functionality, like Debug.Assert, which has a Conditional("DEBUG") on it.

Conditional takes a string argument. If that string is defined (as determined by the compiler's preprocessor), then the compiler emits the method call. If the symbol is not defined, C# still compiles the method, but does not compile the calls.

And later, to strengthen our point:

The Conditional attribute is entirely handled by the compiler without any cooperation from the runtime. The method is still jitted normally, but the compiler just doesn't emit the calls if the symbol is not defined.

Now, we have a conflict. We can't pass parameters to the compiler pre-processor at run-time (to tell it if "DEBUG" is defined or not), only at compile-time, but the method will only be invoked at run-time, because that's when we'll infer the type of our dynamic value.

That's why the binder yells at run-time that this method can't actually be invoked, because that would be breaking the ConditionalAttribute.


https://stackoverflow.com/a/28563248/241446
Кодом людям нужно помогать!
Debug.Assert(dynamic)
От: snaphold  
Дата: 18.05.19 18:55
Оценка: 3 (1)
Внешний сервис возвращает джейсон объект и надо проверить результат на наличие значения.
Как лучше?

Делаю так Debug.Assert(dynamic) — кидает ошибку Cannot dynamically invoke method 'Assert' because it has a Conditional attribute
Re: Debug.Assert(dynamic)
От: pugv Россия  
Дата: 20.05.19 11:48
Оценка:
Здравствуйте, snaphold, Вы писали:

S> Делаю так Debug.Assert(dynamic) — кидает ошибку Cannot dynamically invoke method 'Assert' because it has a Conditional attribute


Скастить к object?
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.