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

How to generate a GUID in XSLT transformations

Posted on Wednesday, May 10, 2017 by Nicki

A recent task involved migrating tens of jobs from Control M to VisualCron. I 'm never in the mood for manual repetitive tasks, so the approach was taken to do it with XSLT, since both Control M and VisualCron support XML config schemas.

VisualCron expects various Ids to be GUIDs, and XSL has no built-in functionality to generate GUIDs, so a plan had to be made. I queried Google and found that one can write extension objects in C# and use them during the translation.

The code for to generate a GUID is very simple:

    public class TransformUtils
    {
        public string GenerateGuid()
        {
            return Guid.NewGuid().ToString();
        }
    }

This is then plugged into the transformation code like this:
            string xmlPath = txtXMLSource.Text;
            string xslPath = txtXSLSource.Text;
            StringBuilder output = new StringBuilder();

            
            XsltArgumentList arguments = new XsltArgumentList();
            arguments.AddExtensionObject("EPS:TransformUtils", new TransformUtils());
            
            using (StringWriter writer = new StringWriter(output))
            {
                XslCompiledTransform transform = new XslCompiledTransform();
                transform.Load(xslPath);
                transform.Transform(xmlPath, arguments, writer);
            }
Inside the xslt stylesheet the extension function can now be called:
<xsl:value-of select="TransformUtils:GenerateGuid()" />

I found a good sample and guide here

C# - How to access a generic typed property

Posted on Thursday, August 15, 2013 by Nicki

My challenge for today was to get the value of generic typed properties from a generic type. Here is the code that worked.

pendingRequest is a generic-typed instance of RequestResponseAsyncResult where the Request and Response properties are generic.

First we have to get the generic type definition:

Type requestType = pendingRequest.GetType().GetGenericTypeDefinition();
Next we get the generic type arguments:
Type[] typeArgs = requestType.GetGenericArguments();
Then we create a generic base type from our generic type:
Type genericType = typeof(RequestResponseAsyncResult<,>);
The next step is to created a generic type according to the type arguments:
Type constructed = genericType.MakeGenericType(typeArgs);
Now we are able to get the property information:
PropertyInfo pireq = pendingRequest.GetType().GetProperty("Request");
PropertyInfo piresp = pendingRequest.GetType().GetProperty("Response");
Finally we can get our properties using reflection:
object request = pireq.GetValue(pendingRequest, null);
object response = piresp.GetValue(pendingRequest, null);



Generic classes and Component services

Posted on Thursday, July 30, 2009 by Nicki

I recently wrote a lightweight generic data access layer with generic list functionality. Everything worked fine until I had to implement a derived class as a Serviced Component. Turns out classes with generic methods cannot be added to component services, regsvcs.exe gives the following error


An unknown exception occurred during installation:
1: System.Reflection.ReflectionTypeLoadException - Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.


Searched Google, and found 2 hits. If that happens you know trouble is looming on the horizon...

It appears to be a known bug, see the details here.

So for now I had to implement a workaround by not having my class derive from the generic base class but instead to have a private instance of it.

C# generics and testing

Posted on Tuesday, July 14, 2009 by Nicki

The power of C# generics, you just gotta love it.


I'm writing NUnit unit tests for the business objects of the application. All the factory objects happen to have a LoadById method that takes an int as parameter, and return a typed list.

This is beautiful, as I can write a generic test method that for the factory type and list type, and then just write a one-liner test for every factory I want to test.

The generic method:

public void TestGeneric(int logicalRecord) where L : new()
{
L list = new L();
Type x = typeof(L);
MethodInfo LoadMethod = x.GetMethod("LoadById");
List records = (List)LoadMethod.Invoke(list, new object[] { logicalRecord });
Assert.IsTrue(records.Count > 0);
}



Invoking the generic method with the Factory type and the List type
            TestGeneric(logicalRecord);


You just gotta love generics. More info here