CodePaste Logo
New Snippet New Recent Snippets Recent Snippets My Snippets My Snippets My Favorites Favorites Web Code Search Snippets Search
Sign inor Register
Language: csharp

Dynamic Weirdness - http://stackoverflow.com/questions/7562205/why-is-an-expandoobject-breaking-code-that-otherwise-works-just-fine

207 Views   
using System; using System.Data.Common; using System.Dynamic; namespace DynamicWeirdness { class Program { static void Main(string[] args) { DynamicWeirdness(); } public static dynamic DynamicWeirdness() { dynamic ex = new ExpandoObject(); ex.Query = "SELECT * FROM Products"; using (var conn = OpenConnection()) { DbCommand cmd = CreateCommand(ex); // <-- DON'T USE VAR cmd.Connection = conn; } Console.WriteLine("It worked!"); Console.Read(); return null; } static DbCommand CreateCommand(dynamic item) { return DbProviderFactories.GetFactory("System.Data.SqlClient") .CreateCommand(); } static DbConnection OpenConnection() { return DbProviderFactories.GetFactory("System.Data.SqlClient") .CreateConnection(); } } }
by Jon Galloway
  September 26, 2011 @ 5:11pm
Add a comment


Report Abuse
brought to you by:
West Wind Techologies