Querying Network Status in .NET programming

Work with `My` namespace

We can use My.Computer.Network.IsAvailable in Visual Basic.NET to determine whether the network is connected. The definition of this property is:

        '''**********************************************************************
        ''';IsAvailable
        ''' <summary>
        '''  Indicates whether or not the local machine is connected to an IP network.
        ''' </summary>
        ''' <value>True if connected, otherwise False</value>
        ''' <remarks></remarks>
        Public ReadOnly Property IsAvailable() As Boolean
            Get

                Return NetInfoAlias.NetworkInterface.GetIsNetworkAvailable()

            End Get
        End Property

It leads to the approach in C#. Continue reading “Querying Network Status in .NET programming”

Q&A

You can put whatever you'd like to ask (technical, life, etc.) here in the comment area.

Click on the title of the article to navigate into it, if necessary, and then submit in the comments area below.

Some debugging tips on Linq.Expression

Machine Translation Warning:

The content below is mostly translated by machine, and I’ve not got enough time to review the text. So, maybe you’d like to view the Simplified Chinese version of the page.

You can generate IL code, and thereby construct a methods, using ‘System.Linq.Expression’ during runtime.Compared to ‘System.Reflection.Emit.ILGenerator’, use expression trees to construct dynamic methods is an easier way to handle IL code.As you know, IL instructions is just like any kind of assemly, which is rather elaborate. I just cannot handle it well enough 🙁 Continue reading “Some debugging tips on Linq.Expression”

Play with Linq.Expression

Suddenly Visual Basic 6.0 ……

Yesterday once more.

When ConditionalExpression.ToString() is invoked...
When ConditionalExpression.ToString() is invoked…

And

Lambda……
Lambda function generated by Linq.Expression.

There’s virtually no way to debug into it…

So I had to use something like `Debug.Print`. Really had a hard time.

So I decided to implement XmlSerializer “again”

Reinventing the wheel T_T

ref = CXuesong/XSerializer/

Pull is welcomed T_T if you’d like to

Recently I’ve been considering the following problems:

When serializing using `XmlSerializer` to get more control on what the XML should look like,

  1. How to serialize members with type `Nullable<T>`, persisting it as XML attribute (rather than element)?
  2. How to serialize references, avoiding it persisted as multiple identical copies of one object?
  3. How to achieve the these two things above, without introducting public auxiliary properties?
    (e.g. for case 1, an auxiliary property can be introduced to do the conversions between `Nullable<T>` and `string`, the latter of which can be persisted as XML attribute.)

It seems that case 2 can be solved using `DataContractSeriallizer`. However, `DataContractSeriallizer` generates Id by itself, and represent Id and its references with `z:Id` and`z:Ref`. As I’ve mentioned in previously (在 XmlSerializer 与 DataContractSerializer 之间抉择), `DataContractSeriallizer` is rather rigid when you want to control the appearance of XML tree it generates.

Then, how to solve these problems?

Maybe, I’ll reinvent the wheel.

See Also

c# – Is there any way for my class to support serialisation as an XML attribute? – Stack Overflow

The exapmle provided in section “Serializing Derived Classes”(with XmlArrayItemAttribute) may be not the case

cite=https://msdn.microsoft.com/EN-US/library/vstudio/2baksw0z.aspx

Another use of the XmlArrayItemAttribute is to allow the serialization of derived classes. For example, another class named Manager that derives from Employee can be added to the previous example. If you do not apply the XmlArrayItemAttribute, the code will fail at run time because the derived class type will not be recognized. To remedy this, apply the attribute twice, each time setting the Type property for each acceptable type (base and derived).

public class Group{
    [XmlArrayItem(Type = typeof(Employee)),
    XmlArrayItem(Type = typeof(Manager))]
    public Employee[] Employees;
}
public class Employee{
    public string Name;
}
public class Manager:Employee{
    public int Level;
}

A serialized instance might resemble the following.

<Group>
<Employees>
    <Employee>
        <Name>Haley</Name>
    </Employee>
    <Employee xsi:type = "Manager">
        <Name>Ann</Name>
        <Level>3</Level>
    <Employee>
</Employees>
</Group>

However, this is not the case.
I used the following test case:

using System;
using System.IO;
using System.Xml.Serialization;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            var f = new XmlSerializer(typeof (Group));
            using (var sw = new StringWriter())
            {
                f.Serialize(sw, new Group()
                {
                    Employees =
                    new [] {
                        new Employee {Name = "Jerry"},
                        new Manager {Name = "Tom", Level = 20}
                    }
                });
                Console.WriteLine(sw.ToString());
            }
        }
    }

    public class Group
    {
        [XmlArrayItem(Type = typeof (Employee)),
         XmlArrayItem(Type = typeof (Manager))] public Employee[] Employees;
    }

    public class Employee
    {
        public string Name;
    }

    public class Manager : Employee
    {
        public int Level;
    }
}

which yields the result:

<?xml version="1.0" encoding="utf-16"?>
<Group xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Employees>
    <Employee>
      <Name>Jerry</Name>
    </Employee>
    <Manager>
      <Name>Tom</Name>
      <Level>20</Level>
    </Manager>
  </Employees>
</Group>

As you can see, `xsi:type` attribute didn’t appeared in the XML document containing the serialized `Group` instance. Instead, the element name of derived `Manager` class is employed.
If I modify the definition of `Group` class to

    [XmlInclude(typeof(Manager))]
    public class Group
    {
        [XmlArrayItem(Type = typeof (Employee))] public Employee[] Employees;
    }

Then I get the following result

<?xml version="1.0" encoding="utf-16"?>
<Group xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Employees>
    <Employee>
      <Name>Jerry</Name>
    </Employee>
    <Employee xsi:type="Manager">
      <Name>Tom</Name>
      <Level>20</Level>
    </Employee>
  </Employees>
</Group>

which coincides with the example output stated on MSDN.
p.s. I tested the code above in VS 2013.

GoG

It’s almost time to post something about Guardians of Ga’Hoole.
Yet before that, let me sleep…

Site Construction Has Reached its Milestone

It took me nearly a week.

I’ve failed to find a new site as a substitution to Baidu Hi in the year 2005.

So… I’m going to publish some mood & code here, as well as pictures. Please tell me / leave a comment if you’re interested in any of them. If this is the case, please login for comments.

The site now is open for registration. You can find login button at the bottom of sidebar, and Sign Up button is below the login form. However in order to fight against spamming, I’ll close the registration in time.

Okay… So I finally managed to translate the post… :-/

And spammers arrived earlier than my expectation, so please, do not use yahoo mail when registering your account. ‘Cause most of the spammers use this E-mail domain so I decided to forbid it. I just have no other way to fight against them.

Content is available under CC BY-SA 3.0 unless otherwise noted.