Tag: dynamic

dynamic and ExpandoObject

tree_volteretaThe dynamic and ExpandoObject have a relation of love and hate with .NET developers. One side defends strongly typed objects to handle data, so it’s possible to handle a lot of issues at compile time. The other side, are developers used to develop scripts, python, and in other similar languages. They defend the dynamic nature of the current projects, with a lot of Xml, Json and unstructured data, so they prefer to use the more dynamic and functional structures of C#. Both are right! Some like to live strong typed and predictable, others like to live on the edge!

Personally, I am a hybrid developer, I like the strong type of languages, but love python and dynamic freedom, like C# provides. So it’s usual for me to use strongly typed objects on my projects, but every time I have to deal with unstructured data, I use the dynamic word and other constructs like Tuple class.

Because sometimes there are too many DTOs on the project!

ExpandoObject Basic Usage

dynamic myGuessObject = new ExpandoObject();

myGuessObject.Name = "Pepe Ribas";
myGuessObject.Address = new ExpandoObject();
myGuessObject.Address.Line1 = "Horizon Street";
myGuessObject.Address.Number = 23;
myGuessObject.Country = new ExpandoObject();
myGuessObject.Country.Code = "PT";
myGuessObject.Country.Name = "Portugal";

// Basic Stuff
Console.WriteLine($"{myGuessObject.Name}");
Console.WriteLine($"{myGuessObject.Address.Line1}, {myGuessObject.Address.Number}");
Console.WriteLine($"{myGuessObject.Country.Code} - {myGuessObject.Country.Name}");

ExpandoObject with methods

dynamic dynamicCalculator = new ExpandoObject();

dynamicCalculator.Add = new Func((v1, v2) => v1 + v2);
dynamicCalculator.Sub = new Func((v1, v2) => v1 - v2);
dynamicCalculator.Mul = new Func((v1, v2) => v1 * v2);
dynamicCalculator.Div = new Func((v1, v2) => v1 / v2);
dynamicCalculator.Mod = new Func((v1, v2) => v1 % v2);

Console.WriteLine($"7 + 8 = {dynamicCalculator.Add(7,8)}");
Console.WriteLine($"7 - 8 = {dynamicCalculator.Sub(7, 8)}");
Console.WriteLine($"7 * 8 = {dynamicCalculator.Mul(7, 8)}");
Console.WriteLine($"7 / 8 = {dynamicCalculator.Div(7, 8)}");
Console.WriteLine($"7 % 8 = {dynamicCalculator.Mod(7, 8)}");

Reflection on the ExpandoObject

dynamic dynamicReflection = new ExpandoObject();
 
dynamicReflection.Name = "Pepe Ribas";
dynamicReflection.Address = new ExpandoObject();
dynamicReflection.Address.Line1 = "Horizon Street";
dynamicReflection.Address.Number = 23;
dynamicReflection.Country = new ExpandoObject();
dynamicReflection.Country.Code = "PT";
dynamicReflection.Country.Name = "Portugal";
 
var typedDynamic = (IDictionary) dynamicReflection;
Console.WriteLine($"Name = {typedDynamic["Name"]}");
var addressEO = (IDictionary)(IDictionary)dynamicReflection.Address;
Console.WriteLine($"Address Line1 = {addressEO["Line1"]}");

Silverlight 4–Out of Browser Authentication

One of the things that was very difficult to find, even in silverlight forums, was a way to know, in a simple way, what user is authenticated when a Silverlight 4 application is running Out-Of-Browser. I’ve found many information about using WCF, RIA, etc, buts it was not what I wanted. So, I investigated a little more about trying to find out how isolated was Silverlight from windows.

At first sight, Silverlight should act as flash, isolated  as possible from the OS, but when running in Windows (as out-of-browser, thank you Microsoft for this “Air” copy”), I should access windows stuff, since I’m exclusively running in Windows, so the isolated thing doesn’t matter anymore! Wrong! I can access only some windows stuff! The .NET framework for Silverlight it’s a little bit “small” in features!

My first try, was to connect to SharePoint using ClientContext from Sharepoint Client API for Silverlight (My application needs some SharePoint list items). When inside SharePoint I just ask about “who am I”, and SharePoint , running in a windows domain, should respond to me with the authenticated user! Was successful! But with some hick ups every time someone from systems staff changed IIS Windows Authentication configuration or some SharePoint tweaks. The problem: I want to run an application and I need to use a SharePoint just to ask about “Who I am”? Yes, you are right! As we say in Portugal, “A cannon to kill a fly”.

Since my Silverlight app its running out of browser I needed to find a way to use directly windows. I thinks it’s a logic thinking!

When a Silverlight App its running outside browser with Elevated Privileges it can access COM stuff! Well, with this “opening”, the sky it’s the limit!

Thanks to “dynamic” word from C# we can do this:

            using (dynamic ad = AutomationFactory.CreateObject("ADSystemInfo"))
            {
                var usernamedn = ad.UserName;
                var computernamedn = ad.ComputerName;
                var DomainDNSNamedn = ad.DomainShortName;

                string us = usernamedn.ToString();
                us = us.Substring(3, us.IndexOf(',') - 3);
                username = string.Format(@"{0}\{1}", DomainDNSNamedn, us);
            }

As you may see, I use a COM Object that returns Active Directory information, including the User logged in.  (OK, the format it’s the AD CN=xxx,CN=xxx, but I now we have a door to the windows, very useful!

Bad Behavior has blocked 86 access attempts in the last 7 days.

Hyper Smash