Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Wednesday, November 20, 2013

read only property and XmlSerializer

I had an issue serializing a class using the XmlSerializer, where the class had a collection property.

example:

public List<Employee> Subordinates { getprivate set; } 

if the property setter was public then everything works fine and the property serialized .
but if the property has a private setter then the following exception is thrown

Unable to generate a temporary class (result=1). error CS0200: Property or indexer '[namespace].Subordinates' cannot be assigned to -- it is read only

The problem was that the class exposed the collection property as a readonly property, but the XmlSerializer needs the property to be read/write access

so the solution is to make the property with public setter as follows
public List<Employee> Subordinates { getset; }  

Wednesday, December 28, 2011

ActiveX controls at asp.net


As web application developers, we want to give our users a highly functional application. We want to provide our users with functionality like printing ,scanning images etc., but it is difficult due to the security and the disconnected architecture of the Internet .

1. create an activeX control
   a. create interface
     public interface ITest
    {
        string Name { get; }
        string SayHello(string str);
    }
    b.create class
     [Guid("a991dd50-ff67-47b0-a4c3-f2b3e9fbd0f4")]
    [ComVisible(true)]
    [ProgId("Test.Test")]
    public class TestClass:ITest
    {
         public string Name
        {
            get  { return "My Test Name"; }
        }
        public string SayHello(string str)
        {
            return "Hello " + str;
        }

    }
   c.register the activeX control:
      at the project properties window => build => check => "Register for COM interop"
2.Host control at wweb application
    a. add object tag to the html page
      <object classid="CLSID:a991dd50-ff67-47b0-a4c3-f2b3e9fbd0f4" id="myCtrl" />
    b. access the object and the functions/properties from javascript
      var myAx = new ActiveXObject("Test.Test");    => the name of ProgId
            alert(myAx.Name);  

   an example for the html:
     <script type="text/javascript">
        function getProperty() {
            var myAx = new ActiveXObject("Test.Test");
            alert(myAx.Name);
        }
        function getName() {
            var myAx = new ActiveXObject("Test.Test");
            var text = document.getElementById("myName").value;
            alert(myAx.GetMe(text));
        }
    </script>
    <table>
        <tr>
            <td>
                <input type="text" id="myName" />
            </td>
            <td>
                <input type="button" value="Property" onclick="getProperty()" />
            </td>
            <td>
                <input type="button" value="Function" onclick="getName()" />
            </td>
        </tr>
    </table>
    <object classid="CLSID:a991dd50-ff67-47b0-a4c3-f2b3e9fbd0f4" id="myCtrl" />