Tag Archives: C#

PDF to Text with .NET

Pdfs are a very ubiquitous and useful file type, but they can be a pain to work with programatically

PDFs are extensively used in my organization, and people always want programs that will extract information from them. It can be very difficult to get the information they want due to the strange format, but sometimes it’s a necessity. Here is how to get a PDF into text, from there you are on your own!

Extract integers from alpha numeric value

static string ExtractNumbers(string Expression)
{
return string.Join(null,System.Text.RegularExpressions.Regex.Split(Expression, “[^\\d]“));
}

Compare 2 objects in c#

public static bool PublicInstancePropertiesEqual(T self, T to, params string[] ignore) where T : class
{
if (self != null && to != null)
{
Type type = typeof(T);
List ignoreList = new List(ignore);
foreach (System.Reflection.PropertyInfo pi in type.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance))
{
if (!ignoreList.Contains(pi.Name))
{
object selfValue = type.GetProperty(pi.Name).GetValue(self, null);
object toValue = type.GetProperty(pi.Name).GetValue(to, null);

if (selfValue != toValue && (selfValue == null || !selfValue.Equals(toValue)))
{
return false;
}
}
}
return true;
}
return self == to;
}

Serialize Enumerable list in Web Service

You will get this error :

“Cannot serialize interface System.Collections.Generic.IEnumerable”

To solve this problem, simply convert the IEnumerable Object to Array using ToArray() extension method.

Private Constructor and Uses

When you declare a Constructor with Private access modifier then it is called Private Constructor. We can use the private constructor in singleton pattern.

If you declare a Constructor as private then it doesn’t allow to create object for its derived class, i.e you loose inherent facility for that class.

Example:

Class A

{

// some code

Private Void A()

{

//Private Constructor

}

}

Class B:A

{

//code

}

B obj = new B();// will give Compilation Error