Linq – deferred ^ immediate execution

update — java 8 streams …

(I feel this is a low-level but popular question…)

– if an operation returns a single value (aka aggregates) then ImmEx. ([[c# indepth]]  P281)

– if an operation returns another sequence, then defEx ([[c# indepth]]  P281)

– ToList() — ImmEx

– sort uses ImmEx in SQL. Ditto Linq — [[c# indepth]]  P369

– regular SQL Select with multiple output rows? Uses defEx. As you retrieve one row, all subsequent rows are subject to change.

– SQL Select with a single-row output? probably ImmEx.

c# attribute on method arg – usage

I feel attributes on method param is a rarely used feature. I feel it is meant for reflection or documentation. http://realfiction.net/go/108 presents a simple usage of this feature, whereby runtime reflection extracts the additional information contained in the attribute value. You can then act on the information.

It also shows that an override method can “add” such attributes.

argument validation — http://www.codinginstinct.com/2008/05/argument-validation-using-attributes.html presents a practical usage — argument validation. For example, you can mark an argument as NotNull

Here’s another usage, but I don’t remember if I tested it successfully.

Make ViewModel get key events only when its host tab is on top

In a tabbed GUI, I have a distinct ViewModel object for each tab. For e.g., tab3 is backed by vm3 object. In it I have a keyboard event handler to manipulate a lot of private data in the vm3 object (and update screen). This handler should fire when Tab 3 is on top. However, vm3 object has no way to tell if Tab3 is on top. MVVM in action.

My Solution: the “code-behind” Mainpage.xaml.cs ctor to register a private this.Handle3 to the this.KeyUp event, where this.Handle3 has access to the Tab3 object, so long as Mainpage.xaml exposes the Tab3 as a field via x:Name.

Handle3() checks if Tab3 is on top and then directly calls vm3.handler. Feasible because Mainpage.xaml.cs has a reference to vm3.

This relies on the code-behind as the melting pot. A quick and dirty solution.

an event inherited from interface->abstract class->concrete class

In this class hierarchy, the event is declared in interface ICommand, then declared abstract in the abstract class, finally implemented in the concrete classes.
Note the abstract must declare it.
    abstract public class TanBinRelayCommand: ICommand{
        private Action methodToExecute;
        private Func<bool> isEnabled;
        public TanBinRelayCommand(Action methodToExecute, Func<bool> isEnabled)
        {
            this.methodToExecute = methodToExecute;
            this.isEnabled = isEnabled;
        }
        #region ICommand members
        ///

        /// who subscribes to this event? I think the command binding (or other objects in the xaml)
        ///

        bool ICommand.CanExecute(object obj) {
            if (this.isEnabled == null) return true;
           
            return this.isEnabled.Invoke();
        }
        void ICommand.Execute(object obj)
        {
            if (this.methodToExecute != null)
                this.methodToExecute.Invoke();
        }
        abstract public event EventHandler CanExecuteChanged;
       

a simple c# SaxParser – NamespaceTolerant

Note: like an istream, the reader has an internal position pointer. reader.Name can return the name of either an element or an attribute, depending on the position.
Note: one common iteration idiom uses Read(), which advances the pointer to the next start/end tag??.  Like a subway express train, it won’t stop at the attributes – local stops.
        ///

        /// It’s not easy to find a SAX parser in c#
        /// It’s not easy to find a xml parser that tolerates undeclared namespaces
        ///
        /// Here’s a solution
        ///

        ///
        private static void SaxParser_NamespaceTolerant(string arg_xmlFullStr)
        {
            using (XmlTextReader reader = new XmlTextReader(
                                         new StringReader(arg_xmlFullStr)))
            {
                reader.Namespaces = false; //ignore undeclared namespaces
                while (reader.Read())
                {
                    if (string.IsNullOrWhiteSpace(reader.Name)) continue;
                    if (reader.IsStartElement())
                    {
                        Dump(reader.Name);
                        if (reader.HasAttributes)
                            while (reader.MoveToNextAttribute())
                                Dump(“\t” + reader.Name + “=” + reader.Value);
                    }
                }
            }
        }

c# DYNAMIC – TryCall_method1 on an Unidentified object#src

With dynamic type, we can try-call an arbitrary method m1(), which need not exist in any public interface. Behaviour — If the underlying type exposes m1, it is invoked. If unavailable you get a run time error, not a compile time error.

One usage idea — Instead of calling the standard ToString() on the unknown object, we want to try-call ToStringXml(), and pray the underlying type supports it. Traditionally, If we have a few types to add ToStringXml(), we would prefer compile-time type check by introducing an interface IXmlDumper. However, if you have 55 types that could pass into TryCall() and 33 of them exposes ToStringXml(), you may need to cast to IXmlDumper then call ToStringXml(). Worse, you would need to edit source code for the 33 types!

With dynamnic type, basically the compile-time type check is postponed to run time. Compile time type check would need m1() declared in the type C, like —

C c = new C(); c.m1();

But if we use dynamic, and type-check at run time, then we don’t need m1() exposed on any public interface —