.NET
  • Accesarea prin reflection a unei metode statice dintr-o clasa parinte
MethodInfo mi = type.GetMethod("GetId", BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Static);
  • Assembly-uri care nu se copiaza in directorul dorit
    • Daca assembly-ul A il referentiaza pe B, iar B il referentiaza pe C, dar nu foloseste nici unul din membrii lui C, atunci C nu va fi copiat in directorul lui A, chiar daca e setat cu Copy Local pe true. Pentru a rezolva problema, trebuie folosit un membru al C in assembly-ul B (si prin folosit vreau sa spun folosit, adica fara warning-uri ca nu e folosit - daca sunt warning-uri, atunci va fi ignorat in caz de compilare in Release).
  • DateTime.ToString
    • Daca trebuie specificat un format astfel incat sa nu intoarca nimic, atunci incearca dateTime.ToString("\"<text ad-literam>\"");
  • Generic constraints
    • struct, class
    • new()
    • base class
  • Schimbarea unui tip in altul
    • TypeConverter
    • Convert.
<runtime> 
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> 
                <dependentAssembly> 
                        <assemblyIdentity name="NHibernate" 
publicKeyToken="aa95f207798dfdb4" culture="neutral"/> 
                        <bindingRedirect oldVersion="2.0.0.4000" newVersion="2.0.1.4000"/> 
                </dependentAssembly> 
        </assemblyBinding> 
</runtime>
  • Cum sa ascunzi o metoda a unei librarii intr-o versiune noua
    • [EditorBrowsable(EditorBrowsableState.Never), Obsolete("Expect has been renamed to Setup.", false)]
    • Asa nu va mai aparea in Intellisense, si utilizatorul va primi si un mesaj cum ca e obsolete.
  • Query-uri XPath in xml-uri care folosesc namespace-uri.
    • Definesc namespace-urile intr-un XmlNamespaceManager, pe care il folosesc apoi in document.SelectNode
var document = new XmlDocument();
document.LoadXml(xml);
 
var namespaceManager = new XmlNamespaceManager(document.NameTable);
namespaceManager.AddNamespace("ogc", "http://www.opengis.net/ogc");
 
var valueNode = document.SelectSingleNode("ogc:Filter/ogc:And/ogc:PropertyIsEqualTo[ogc:PropertyName='incidentId']/ogc:Literal", namespaceManager);
  • Crearea unui serviciu Windows
    • Dupa ce am creat proiectul pentru serviciu, intru in design mode pe serviciu -> click dreapta pe designer -> Add Installer
    • Intuitive, huh?
  • Servicii Windows
    • Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory;
  • Extragerea configuratiei pentru log4net in alt fisier
    • <add key="log4net.Config" value="config\log4net.config"/>
  • Custom configSections
    • Pot defini un sectionGroup, continand mai multe sectiuni de tip SingleTagSectionHandler (sunt mai multe SectionHandler-e disponibile in System.Configuration)
    • Obtin sectiunea respectiva cu ConfigurationManager.GetSection("email/incoming") - pentru SingleTagSectionHandler va intoarce un Hashtable cu cheile si valorile asociate lor.
    • Un articol interesant: http://www.codeproject.com/KB/dotnet/mysteriesofconfiguration.aspx
<sectionGroup name="email">
    <section name="incoming" type="System.Configuration.SingleTagSectionHandler"/>
    ...
</sectionGroup>
<email>
    <incoming
        server="[server]"
        port="110"
        username="[username]"
        password="[password]"
    />
</email>
<email>
    <incoming configSource="config\incoming-email.config" />
    <outgoing configSource="config\outgoing-email.config" />
</email>
  • Regex.Replace - backreferences
Regex.Replace("Hello World!", "(?<vowel>[aeiouAEIOU])", "_${vowel}_")
  • Alternativa win client la HttpContext.Current
    • CallContext.GetData/SetData.
  • Missing .NET performance counters (procmon ii raporteaza ca fiind corupti)
  • Configurarea trimiterii de e-mailuri astfel incat acestea sa fie salvate intr-un director in loc sa fie trimise
  <system.net>
    <mailSettings>
      <smtp deliveryMethod="SpecifiedPickupDirectory" from="no_reply@example.com">
        <specifiedPickupDirectory pickupDirectoryLocation="D:\MyEmailsFolder" />
      </smtp>
    </mailSettings>
  </system.net>
  • Getting the base url for an uri
    • uri.GetLeftPart(UriPartial.Authority)
  • Redenumirea unui nod xml
    internal static class XmlDocumentExtensions
    {
        internal static void RenameNode(this XmlDocument document, string xpath, string newName)
        {
            var elements = document.SelectNodes(xpath);
 
            foreach (XmlElement element in elements)
            {
                var newElement = document.CreateElement(newName);
                element.ParentNode.ReplaceChild(newElement, element);
 
                foreach (XmlAttribute attribute in element.Attributes)
                    newElement.Attributes.Append((XmlAttribute)attribute.Clone()); //probabil ar fi mai eficient sa fac append la atributul initial in loc sa fac clone, si sa tin cont de asta in foreach (i.e. sa fac un while :) )
 
                foreach (XmlNode child in element.ChildNodes)
                    newElement.AppendChild(child.Clone());
            }
        }
    }
  • The request was aborted: Could not create SSL/TLS secure channel
    • In cazul meu, am rezolvat problema dandu-i drepturi de acces la cheia privata a certificatului client utilizatorului meu (e ciudat, pentru ca eu sunt admin si adminii aveau deja drepturi de acces la cheia privata)
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License