Schlagwort-Archive: C#

C# Get Classes by an assembly

Hello  buddies,

today i want to show you a code snippet where you can get all class names of an internal or external library or an assembly. You have to add the library as an Reference.

I am developing in the company a framework, where i need the names of classes of an external library which i added as reference.

We need a using, called System.Reflection.Assembly.

The code to get classes of an assembly:

static void Main(string[] args)
{
ListOfClasses("Company.LibraryNameOrAssemblyName"); //call the method with a library. which you add as reference.
Console.Read();
}
public static string[] ListOfClasses(string name)
{
 
Assembly assembly = Assembly.ReflectionOnlyLoad(name); //create an new object of Assembly type.
string[] classes = new string[assembly.GetTypes().Length]; //get count of types 
 
int counter = 0; //counter for array index, loop through available types
foreach (var classtype in assembly.GetTypes())
{
classes[counter] = classtype.FullName; //add Name of class to array.
counter++; //increase index counter
}
return classes; // return result as array
 
}

If you execute the code you will get back an array filled with classnames from the library.

c# assembly classes

C# get classes of assembly

 

Hope, someone will find it helpfull.

C# Get directory size using LINQ, c# GetDirectorySize

C# Get directory size using LINQ, DirectorySize

Sometimes you need or must know the folder DirectorySize or frin more directories in C#.
In this case you can use this method, which get the folder size in c#, the output size is in bytes.

public static long DirectorySize(string parentDirectory)
{
return new DirectoryInfo(parentDirectory).GetFiles(searchPattern: "*", searchOption: SearchOption.AllDirectories).Sum(file => file.Length);
}

In this linq method you can also set an search pattern and choose if only TopDirectories or AllDirectories will be run through.

C# Serialize object to XML

Gerne werden XML Dokumente verwendet, um Einstellungen oder ähnliches abzuspeichern.
Das Thema  möchte ich euch im folgendem Post näher bringen.

Ziel: Ziel dieses Tutorials ist es in C# eine Serialisierung von XML-Dokumetnen vorzunehmen.

In der objekorientierten Welt erstellen wir uns erstmal ein beliebig epublic Klasse: Person.cs
In diesem Beispiel, erstelle ich Eigenschaften einer Person. Eine Person besitzt gewöhnlich einen Vor- und Nachnamen, Alter, Wohnort, Straße, Postleitzahl usw. Meine Klasse Person hat somit folgend Properties (Consolen Applikation!):

using System;using System.Linq;
namespace XMLSerialization{public class Person{string firstname;string lastname;string city;int postalcode;byte age;string street;string housenumber;int phonenumber;int mobilenumber;string Email;

 

////// Gets or sets the p email.///
////// The p email.///public string PEmail{get{return this.Email;}set{this.Email = value;}}
////// Gets or sets the street.///
////// The street.///public string Street{get{return this.street;}set{this.street = value;}}
////// Gets or sets the firstname.///
////// The firstname.///public string Firstname{get{return this.firstname;}set{this.firstname = value;}}
////// Gets or sets the lastname.///
////// The lastname.///public string Lastname{get{return this.lastname;}set{this.lastname = value;}}
////// Gets or sets the city.///
////// The city.///public string City{get{return this.city;}set{this.city = value;}}
////// Gets or sets the postalcode.///
////// The postalcode.///public int Postalcode{get{return this.postalcode;}set{this.postalcode = value;}}
////// Gets or sets the age.///
////// The age.///public byte Age{get{return this.age;}set{this.age = value;}}
///////// Gets or sets the housenumber.///
////// The housenumber.///public string Housenumber{get{return this.housenumber;}set{this.housenumber = value;}}
////// Gets or sets the phonenumber.///
////// The phonenumber.///public int Phonenumber{get{return this.phonenumber;}set{this.phonenumber = value;}}
////// Gets or sets the mobilenumber.///
////// The mobilenumber.///public int Mobilenumber{get{return this.mobilenumber;}set{this.mobilenumber = value;}}
}}

Ende des Codes der public Klasse Person.cs

Nun müssen wir eine Neue Person (Name Jack) erstellen und der Person Eigenschaften zuweisen, dies wird in unserem Main Aufruf gemacht:

using System;using System.IO;using System.Linq;using System.Xml.Serialization;
namespace XMLSerialization{class Program{static string file;
////// Defines the entry point of the application.///
///The arguments. static void Main(string[] args){///Console.WriteLine(“Filename:”);file = AppDomain.CurrentDomain.BaseDirectory + Console.ReadLine();//Create a new objeft of type PersonPerson personJack = new Person();Console.WriteLine(“Age:”);personJack.Age = Convert.ToByte(Console.ReadLine());Console.WriteLine(“Street:”);personJack.Street = Console.ReadLine();Console.WriteLine(“City:”);personJack.City = Console.ReadLine();Console.WriteLine(“Firstname:”);personJack.Firstname = Console.ReadLine();Console.WriteLine(“Lastname:”);personJack.Lastname = Console.ReadLine();Console.WriteLine(“Housenumber:”);personJack.Housenumber = Console.ReadLine();Console.WriteLine(“Mobile:”);personJack.Mobilenumber = Convert.ToInt32(Console.ReadLine());Console.WriteLine(“Phone:”);personJack.Phonenumber = Convert.ToInt32(Console.ReadLine());Console.WriteLine(“Mail:”);personJack.PEmail = Console.ReadLine();Console.WriteLine(“Postalcode:”);personJack.Postalcode = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(“Press enter to serialize…”);
Console.Read();///Call Serialize method and serialize objedct to xml file.Serialize(personJack);}
////// Serializes the specified person jack.///
///The person jack. static void Serialize(Person personJack){XmlSerializer xmlSer = new XmlSerializer(typeof(Person));using (TextWriter txtwriter = new StreamWriter(file)){xmlSer.Serialize(txtwriter, personJack);}}}}}}

Nun haben wir ein Objekt des Typs Person in  eine XML Datei serialisiert,compilieren wir die Anwendung, müssen wir erst den Dateinnamen angeben z.b.: Jack.xml und anschließend die Eigenschaften schreiben und mit Enter bestätigen.
Eingaben könnten wie folgt aussehen:

Input XML Serialization
Input XML Serialization

Als Ergebnis werden meine Objekte in eine XML-Datei serialisiert, Öffnet man die XML Datei mit einem Browser sehen wir folgendes Ergebnis:

 

XML Result
XML Result

Die Solution könnt ihr um, den Blogeintrag besser nachzuvollziehen hier herunterladen.
Mit dieser Solution könnt ihr das ganze auch wieder deserialisieren. Klick hier.