AXForum  
Вернуться   AXForum > Microsoft Dynamics CRM > Dynamics CRM: Blogs
NAV
Забыли пароль?
Зарегистрироваться Правила Справка Пользователи Сообщения за день Поиск

 
 
Опции темы Поиск в этой теме Опции просмотра
Старый 27.02.2014, 19:03   #1  
Blog bot is offline
Blog bot
Участник
 
25,643 / 848 (80) +++++++
Регистрация: 28.10.2006
A friend asked my by e-mail today about a problem he encountered with DotNet interop: how to access a property of an object, if the property name matches a reserved word in NAV. A simplest example is from the Microsoft.Dynamics.Nav.SMTP.SmtpMessage class that comes bundled with NAV 2013.

Consider this piece of code:



Mail := Mail.SmtpMessage;Mail.To := 'john.doe@noname.no';If you try to compile this, it will fail. It’s simple: the To property name is invalid in the C/AL context, because it is a reserved word.

So, how do you fix it?



The answer is: reflection.

Reflection is a built-in functionality into the Framework Class Library that allows you to access meta-information about .NET types, and to dynamically manage that information. I know this is a lousy definition, and all my .NET black-belt ninja friends will now laugh in my face, but I wanted to keep the definition simple for my C/AL friends who don’t have extensive .NET experience.

So, in principle, this is what you must do if you have a situation such as this:
  1. Get the reference to the type of the object with a stubborn property.
  2. Use the type reference to get the PropertyInfo class representing the property you are trying to set.
  3. Use the PropertyInfo instance to dynamically set the value of the property on your object.
Let’s translate it to C/AL now. First, declare a variable of System.Type type from the mscorlib assembly. Then, write this code:



Mail := Mail.SmtpMessage;//Mail.To := 'john.doe@noname.no';Type := GETDOTNETTYPE(Mail);Type.GetProperty('To').SetValue(Mail,'john.doe@noname.no');MESSAGE('%1',Type.GetProperty('To').GetValue(Mail));And that’s it. Try it and see if it works.

You can use the same principle with methods as well. I won’t write any code examples here, because I’ve already posted some examples that showcase this principle. Check the Try..Catch demo from my TechDays 2014 Black Belt session in this post.

I hope this helps. Please, let me know!

Read this post at its original location at http://vjeko.com/blog/dotnet-quick-t...-invalid-names, or visit the original blog at http://vjeko.com. 5e33c5f6cb90c441bd1f23d5b9eeca34</img> </img>

Читать дальше
__________________
Расскажите о новых и интересных блогах по Microsoft Dynamics, напишите личное сообщение администратору.
 


Ваши права в разделе
Вы не можете создавать новые темы
Вы не можете отвечать в темах
Вы не можете прикреплять вложения
Вы не можете редактировать свои сообщения

BB коды Вкл.
Смайлы Вкл.
[IMG] код Вкл.
HTML код Выкл.
Быстрый переход

Рейтинг@Mail.ru
Часовой пояс GMT +3, время: 03:05.