dynamic-keyword

F#: Is there a way to extend the monad keyword list?

北城余情 提交于 2019-12-31 08:11:40
问题 Inside an F# monad, if you say let! , the compiler translates that to a Bind member that you've defined on the monad builder. Now I see there are Query monads, as shown here on MSDN, where you can say: query { for student in db.Student do select student count } and the select and count , for example, will be translated to the QueryBuilder members Linq.QueryBuilder.Select and Linq.QueryBuilder.Count. My question is, is this mapping of keywords to members hardwired into the F# compiler, or is

Using dynamic in C# to access field of anonymous type - possible?

早过忘川 提交于 2019-12-23 10:16:14
问题 I've got a controller method: public JsonResult CalculateStuff(int coolArg) { if(calculatePossible) return Json(CoolMethod(coolArg)); else return Json(new { Calculated = false }); } Now, I'd like to test this. public void MyTest { var controller = GetControllerInstance(); var result = controller.CalculateStuff().Data as dynamic; Assert.IsTrue(result.Calculated == false); } This throws a RuntimeBinderException saying that Calculated is not defined. Is there any way to achieve this? UPDATE

F#: Is there a way to extend the monad keyword list?

三世轮回 提交于 2019-12-02 15:35:30
Inside an F# monad, if you say let! , the compiler translates that to a Bind member that you've defined on the monad builder. Now I see there are Query monads, as shown here on MSDN , where you can say: query { for student in db.Student do select student count } and the select and count , for example, will be translated to the QueryBuilder members Linq.QueryBuilder.Select and Linq.QueryBuilder.Count . My question is, is this mapping of keywords to members hardwired into the F# compiler, or is it extensible? For example, can I say something like: FooMonadBuilder() { bar } and somehow tell the F

C# Using the Dynamic keyword to access properties via strings without reflection

99封情书 提交于 2019-11-30 12:38:06
I would like to write something similar to the following: // I will pass in a number of "properties" specified as strings that I want modified string[] properties = new [] { "AllowEdit", "AllowDelete" }; // Casting the component I'm using to a dynamic object of some sort ? dynamic d = myGridComponent; // Iterate over the strings and set the properties foreach(var s in properties) { //d.s = true; // //d[s] = true; // this format would be ideal } I was wondering if there was an easy way to do this without using Reflection [ .GetProperty(...).GetValue(...,...) ] using the new C# 4.0 keyword:

How can I create a sequence of numbered variables at run time?

血红的双手。 提交于 2019-11-30 10:10:10
问题 Friends, I must create a series of ArrayList s, each containing objects of unknown origin , with each instance assigned to a separate local variable. So far, so good... But I also need each local variable's name to follow a very specific pattern: the name should begin with "oArr", followed by one or more digits reflecting that particular array's position within the sequence. Furthermore, I will not know at compile-time how many of these arrays - and hence, how many local variables - I will be

How can I create a sequence of numbered variables at run time?

牧云@^-^@ 提交于 2019-11-29 18:28:14
Friends, I must create a series of ArrayList s, each containing objects of unknown origin , with each instance assigned to a separate local variable. So far, so good... But I also need each local variable's name to follow a very specific pattern: the name should begin with "oArr", followed by one or more digits reflecting that particular array's position within the sequence. Furthermore, I will not know at compile-time how many of these arrays - and hence, how many local variables - I will be needing! It strikes me that this is perhaps a problem that could be solved by the availability of

C# Using the Dynamic keyword to access properties via strings without reflection

点点圈 提交于 2019-11-29 17:44:26
问题 I would like to write something similar to the following: // I will pass in a number of "properties" specified as strings that I want modified string[] properties = new [] { "AllowEdit", "AllowDelete" }; // Casting the component I'm using to a dynamic object of some sort ? dynamic d = myGridComponent; // Iterate over the strings and set the properties foreach(var s in properties) { //d.s = true; // //d[s] = true; // this format would be ideal } I was wondering if there was an easy way to do

Limitations of the dynamic type in C#

∥☆過路亽.° 提交于 2019-11-28 04:12:07
问题 Could you give me some reasons for limitations of the dynamic type in C#? I read about them in "Pro C# 2010 and the .NET 4 platform". Here is an excerpt (if quoting books is illegal here, tell me and I will remove the excerpt): While a great many things can be defined using the dynamic keyword, there are some limitations regarding its usage. While they are not show stoppers, do know that a dynamic data item cannot make use of lambda expressions or C# anonymous methods when calling a method.

C# 4.0 Dynamic vs Expando… where do they fit?

為{幸葍}努か 提交于 2019-11-27 18:56:58
I am trying to learn all the new goodies that come with C# 4.0. I am failing to understand the differences between the DynamicObject and ExpandoObject types. It seems like DynamicObject is used e.g. when you want to access variables from Python scripts and ExpandoObject when talking with COM/Office objects. Am I right? What is the difference in their use? Expando is a dynamic type to which members can be added (or removed) at runtime. dynamic is designed to allow .NET to interoperate with types when interfacing with dynamic typing languages such as Python and JavaScript. So, if you need to

C# 4.0 Dynamic vs Expando… where do they fit?

99封情书 提交于 2019-11-26 22:47:08
问题 I am trying to learn all the new goodies that come with C# 4.0. I am failing to understand the differences between the DynamicObject and ExpandoObject types. It seems like DynamicObject is used e.g. when you want to access variables from Python scripts and ExpandoObject when talking with COM/Office objects. Am I right? What is the difference in their use? 回答1: Expando is a dynamic type to which members can be added (or removed) at runtime. dynamic is designed to allow .NET to interoperate