Exemplo n.º 1
0
        public static void Demonstrate()
        {
            const int number = 42;
            GetStringRepresentation stringRepresentation = number.ToString;

            Console.WriteLine(stringRepresentation());
        }
Exemplo n.º 2
0
        //Follow a set of methods serving as a configuration for a serializer

        /// <summary>
        /// returns a description of Person
        /// </summary>
        /// <returns></returns>
        static RootDescriptor <Person> GetPersonDescriptor()
        {
            //Create delegates to functions describing Person fields
            //functions are to be called from the Serialize function
            //descritors for each field nested anywhere in Person object is recursivelly created and .Serialize() is called on each descriptor
            GetStringRepresentation <Person> FirstName = new GetStringRepresentation <Person>((Person person, StringBuilder builder) =>
                                                                                              { GetStringDescriptor().Serialize(person.FirstName, nameof(person.FirstName), builder); });
            GetStringRepresentation <Person> LastName = new GetStringRepresentation <Person>((Person person, StringBuilder builder) =>
                                                                                             { GetStringDescriptor().Serialize(person.LastName, nameof(person.LastName), builder); });
            GetStringRepresentation <Person> HomeAddress = new GetStringRepresentation <Person>((Person person, StringBuilder builder) =>
                                                                                                { GetAddressDescriptor().Serialize(person.HomeAddress, nameof(person.HomeAddress), builder); });
            GetStringRepresentation <Person> WorkAddress = new GetStringRepresentation <Person>((Person person, StringBuilder builder) =>
                                                                                                { GetAddressDescriptor().Serialize(person.WorkAddress, nameof(person.WorkAddress), builder); });
            GetStringRepresentation <Person> CitizenOf = new GetStringRepresentation <Person>((Person person, StringBuilder builder) =>
                                                                                              { GetCountryDescriptor().Serialize(person.CitizenOf, nameof(person.CitizenOf), builder); });
            GetStringRepresentation <Person> MobilePhone = new GetStringRepresentation <Person>((Person person, StringBuilder builder) =>
                                                                                                { GetPhoneNumberDescriptor().Serialize(person.MobilePhone, nameof(person.MobilePhone), builder); });

            //create new person descriptor with coresponding array of delegates
            RootDescriptor <Person> rootDesc = new RootDescriptor <Person>(new GetStringRepresentation <Person>[]
                                                                           { FirstName, LastName, HomeAddress, WorkAddress, CitizenOf, MobilePhone });

            return(rootDesc);
            //no Person instance is passed directly to GetDescriptor() functions - Descriptors can be created independentally of a particular instance
            //Person instance is passed to Serialize function when called, then propageted thru corresponding delegates to recursive serialize calls
        }
Exemplo n.º 3
0
        static RootDescriptor <PhoneNumber> GetPhoneNumberDescriptor()
        {
            GetStringRepresentation <PhoneNumber> Country = new GetStringRepresentation <PhoneNumber>((PhoneNumber phoneNumber, StringBuilder builder) =>
                                                                                                      { GetCountryDescriptor().Serialize(phoneNumber.Country, nameof(phoneNumber.Country), builder); });
            GetStringRepresentation <PhoneNumber> Number = new GetStringRepresentation <PhoneNumber>((PhoneNumber phoneNumber, StringBuilder builder) =>
                                                                                                     { GetIntDescriptor().Serialize(phoneNumber.Number, nameof(phoneNumber.Number), builder); });

            return(new RootDescriptor <PhoneNumber>(new GetStringRepresentation <PhoneNumber>[] { Country, Number }));
        }
Exemplo n.º 4
0
        static RootDescriptor <Country> GetCountryDescriptor()
        {
            GetStringRepresentation <Country> Name = new GetStringRepresentation <Country>((Country country, StringBuilder builder) =>
                                                                                           { GetStringDescriptor().Serialize(country.Name, nameof(country.Name), builder); });
            GetStringRepresentation <Country> AreaCode = new GetStringRepresentation <Country>((Country country, StringBuilder builder) =>
                                                                                               { GetIntDescriptor().Serialize(country.AreaCode, nameof(country.AreaCode), builder); });

            return(new RootDescriptor <Country>(new GetStringRepresentation <Country>[] { Name, AreaCode }));
        }
Exemplo n.º 5
0
        static RootDescriptor <Address> GetAddressDescriptor()
        {
            GetStringRepresentation <Address> Street = new GetStringRepresentation <Address>((Address adr, StringBuilder builder) =>
                                                                                             { GetStringDescriptor().Serialize(adr.Street, nameof(adr.Street), builder); });
            GetStringRepresentation <Address> City = new GetStringRepresentation <Address>((Address adr, StringBuilder builder) =>
                                                                                           { GetStringDescriptor().Serialize(adr.City, nameof(adr.City), builder); });

            return(new RootDescriptor <Address>(new GetStringRepresentation <Address>[] { Street, City }));
        }
Exemplo n.º 6
0
        public static void Demonstrate()
        {
            GetStringRepresentation <IEnumerable <int> > collectionStringRepresentation = GetCollectionStringRepresentation;
            GetStringRepresentation <List <int> >        listStringRepresentation       = GetListStringRepresentation;

            listStringRepresentation = collectionStringRepresentation;

            var list = new List <int>();

            listStringRepresentation(list);
        }