Categories
C# Programming Uncategorized

Serialize Size Struct In ProtoBuf-net

The following code allows you to serialise the Size struct when using ProtoBuf-net.

First define a surrogate.

[ProtoContract]
    struct ProtoSize
    {
        [ProtoMember(1)]
        int Width;
        [ProtoMember(2)]
        int Height;
      
        public static implicit operator Size(ProtoSize s)
        { return new Size(new Point(s.Width, s.Height)); }
        
        public static implicit operator ProtoSize(Size s)
        { return new ProtoSize { Width = s.Width, Height = s.Height };  }
    }

Then add it to the RuntimeTypeModel.

static RuntimeTypeModel Model;

Model = RuntimeTypeModel.Create();
Model.AllowParseableTypes = true;
Model.Add(typeof(Size), false).SetSurrogate(typeof(ProtoSize));

Then use it.

 Model.Serialize(someFile, someObjectWithSizeProperty);

Remember to decorate the size property with a [ProtoMember] attribute.

Here is a list of other useful surrogates for Dot Net, which I gleaned from the Internet.


    [ProtoContract]
    struct ProtoColor
    {
        [ProtoMember(1, DataFormat = DataFormat.FixedSize)]
        public uint argb;
        public static implicit operator Color(ProtoColor c)
        { return Color.FromArgb((int)c.argb); }
        public static implicit operator ProtoColor(Color c)
        { return new ProtoColor { argb = (uint)c.ToArgb() }; }
    }
    [ProtoContract()]
    class ProtoFont
    {
        [ProtoMember(1)]
        string FontFamily;
        [ProtoMember(2)]
        float SizeInPoints;
        [ProtoMember(3)]
        FontStyle Style;

        public static implicit operator Font(ProtoFont f)
        {
            return new Font(f.FontFamily, f.SizeInPoints, f.Style);
        }
        public static implicit operator ProtoFont(Font f)
        {
            return f == null ? null : new ProtoFont
            {
                FontFamily = f.FontFamily.Name,
                SizeInPoints = f.SizeInPoints,
                Style = f.Style
            };
        }
    }
    [ProtoContract()]
    class ProtoStringFormat
    {
        [ProtoMember(1, DataFormat = DataFormat.Group)]
        StringAlignment Alignment;
        [ProtoMember(2)]
        StringAlignment LineAlignment;
        [ProtoMember(3)]
        StringFormatFlags Flags;
        public static implicit operator StringFormat(ProtoStringFormat f)
        {
            return new StringFormat(f.Flags)
            {
                Alignment = f.Alignment,
                LineAlignment = f.LineAlignment
            };
        }
        public static implicit operator ProtoStringFormat(StringFormat f)
        {
            return f == null ? null : new ProtoStringFormat()
            {
                Flags = f.FormatFlags,
                Alignment = f.Alignment,
                LineAlignment = f.LineAlignment
            };
        }
    }
Categories
Uncategorized

Setup Hyper-V Network for CentOS Sharing WIFI

After following a number of how-to instructions off the Internet on how to connect my CentOS virtual Machine to my Windows 10 WIFI connection, and failing, I finally managed to get it working in three simple steps.

  1. Create a new Virtual Switch, via the Virtual Switch Manager.

THIS KEPT FAILING until I enabled and then disabled the “Allow other network and users to connect through this computer’s Internet Connection” on my Wifi adapter.

2. Added a new external network adapter to my virtual machine

3. Configured the networking on CentOS

4. That was all that was needed.

Categories
Uncategorized

Windows 10 Console Apps Hanging

Recently I wrote a console app. to run under Windows 10. To my frustration the app appeared to hang for no apparent reason, but would resume when I pressed the Enter key.

After much investigating I found that this is by (Microsoft) design. When you click on the black screen of a console app it goes into “QuickEdit Mode”; basically its waiting for you to paste some text and press enter. This happens the next time the app calls a write/writeline. It can be stopped by clicking the top right of the console app, selecting “Properties” and unticking the “Quick Edit Mode” option.

Categories
Uncategorized

Getting Rid of Indian Myna Birds

If you have a problem with Indian Myna Birds you will probably have found the advice to get a trap and catch them. This is a good solution as it will reduce the population, but it takes some effort. If you want to simply move the birds on, so that they stop pooping all over you house and belongings, I have a very easy way to do that.

Get a water pistol!

I dont mean a little one, but a Super Soaker type arrangement. These birds are increadbly aware of being hunted. It only takes a couple of days of targeting these birds with water, and they will packup and leave. Abiet propably only to your neighbours house – but thats their problem!

Categories
Uncategorized

Completed ServiceNow Developer Course

I just finished a UDEMY Service Now Developer course

https://www.udemy.com/course/servicenow-201-development/learn/lecture/7334002#overview

This was an excellent course, the instructor had clearly been working as a ServiceNow professional and I would recommend this course to anyone who wants to jump into development on the Service Now platform.

The coding portions are all JavaScript, so you need some experience in that language. ServiceNow is such a huge platform that I think being able to develop applications within it’s framework would be necessary for any medium to large company.

Categories
Uncategorized

Welcome

This site is hosted on a $20 RaspberryPi Zero behind my home router without a static IP address. The DNS is provided by the ‘truely’ free dynamic DNS provider DuckDNS.