Monthly Archives: November 2014

How to convert a PCL project into a standard Class Library project

Have you created a Portable Class Library and after some development you discovered that it’s not a PCL, but a normal class library? Then, follow these steps.

Basically, you have to do this:

http://stackoverflow.com/questions/20094075/convert-a-pcl-to-a-regular-class-library

Advertisements

Candidate of Regex that matches Fully Qualified Names

With the help of Juanmi, and after a few polishing, it works!

(?:([\.\D]+\,\s*){1,2}(?i)Version\=(\d+\.){3}\d+\,\s*(?i)Culture\=[\w\-]+\,\s*(?i)PublicKeyToken\=[\w]{16})

For instance, the following qualified names will be matched:

System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System.data, version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
myTypes, Version=1.0.1234.0, Culture=en-US, PublicKeyToken=b77a5c561934e089c

For sure, there will be a lot of cases when the Regex will match incorrectly, but I think it covers most of cases 🙂

ReactiveUI. Excluding already chosen items.

This situation is quite common. Only certain items are available for selection. In the sample, we have 3 hotel rooms and 3 guests. When you assign one guest to a room and want to assign another guest, the first shouldn’t appear on the list of selectable guests. This is done using a derived collection with a filter.

person => !person.IsAdded || (person.IsAdded && Equals(person, SelectedPerson))

Untitled

using System.Collections.Generic;
using System.Linq;
using MEFedMVVM.ViewModelLocator;
using ReactiveUI;

namespace WpfApplication3
{
    [ExportViewModel("Main")]
    public class MainViewModel : ReactiveObject
    {
        public MainViewModel()
        {
            var people = new ReactiveList<Person>
            {
                new Person("Jim"),
                new Person("Tom"),
                new Person("Jack"),
            };
            people.ChangeTrackingEnabled = true;

            Rooms = new ReactiveList<Room>(people.Select((person, i) => new Room(people, string.Format("Room {0}", i))));
        }

        public IReactiveList<Room> Rooms { get; private set; }
    }

    public class Room : ReactiveObject
    {
        private readonly string name;
        private Person selectedPerson;

        public Room(IEnumerable<Person> people, string name)
        {
            this.name = name;
            People = people.CreateDerivedCollection(person => person, person => !person.IsAdded || (person.IsAdded && Equals(person, SelectedPerson)));
        }

        public IReactiveDerivedList<Person> People { get; set; }

        public Person SelectedPerson
        {
            get { return selectedPerson; }
            set
            {
                if (selectedPerson != null)
                {
                    selectedPerson.IsAdded = false;
                }

                if (value != null)
                {
                    value.IsAdded = true;
                }

                this.RaiseAndSetIfChanged(ref selectedPerson, value);
            }
        }

        public string Name
        {
            get { return name; }
        }
    }

    public class Person : ReactiveObject
    {
        private readonly string name;
        private bool isAdded;

        public Person(string name)
        {
            this.name = name;
        }

        public string Name
        {
            get { return name; }
        }

        public bool IsAdded
        {
            get { return isAdded; }
            set { this.RaiseAndSetIfChanged(ref isAdded, value); }
        }

        private bool Equals(Person other)
        {
            return string.Equals(name, other.name);
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != this.GetType()) return false;
            return Equals((Person)obj);
        }

        public override int GetHashCode()
        {
            return name.GetHashCode();
        }
    }
}

Left join in LINQ

Do you want to retrieve people that doesn’t own a pet? Just take a look at this 🙂

using System.Collections.Generic;

namespace LeftJoin
{
    using System;
    using System.Linq;

    class Program
    {
        static void Main(string[] args)
        {
            var johnny = new Person("Johnny");
            var david = new Person("David");
            var michael = new Person("Michael");

            var owners = new List<Person> { johnny, david, michael };
            var pets = new List<Pet> { new Pet("Flocky", johnny), new Pet("Choo choo", david)};

            var noPets = from owner in owners join p in pets on owner equals p.Owner into joined where !joined.Any() select owner;                        

            foreach (var person in noPets)
            {
                Console.WriteLine(person);
            }
        }
    }

    internal class Person
    {
        public Person(string name)
        {
            this.Name = name;
        }

        public string Name { get; set; }

        public override string ToString()
        {
            return Name;
        }
    }

    internal class Pet
    {
        private readonly string name;

        private readonly Person owner;

        public Pet(string name, Person owner)
        {
            this.name = name;
            this.owner = owner;
        }

        public string Name
        {
            get
            {
                return this.name;
            }
        }

        public Person Owner
        {
            get
            {
                return this.owner;
            }
        }

        public override string ToString()
        {
            return Name;
        }
    }
}