milosev.com
  • Home
    • List all categories
    • Sitemap
  • Downloads
    • WebSphere
    • Hitachi902
    • Hospital
    • Kryptonite
    • OCR
    • APK
  • About me
    • Gallery
    • Curriculum vitae
      • Resume
      • Lebenslauf
    • Social networks
      • Facebook
      • Twitter
      • LinkedIn
      • Xing
      • GitHub
      • Google Maps
      • Sports tracker
    • Adventures planning
  1. You are here:  
  2. Home

Invoke private method with reflection

Details
Written by: Stanko Milosev
Category: C#
Published: 17 October 2021
Last Updated: 07 November 2021
Hits: 570
I have added a class library, and introduced one private method. Invoke looks like:
MethodInfoClass mic = new MethodInfoClass();

const BindingFlags bindingFlags = BindingFlags.InvokeMethod |
								  BindingFlags.Public |
								  BindingFlags.NonPublic |
								  BindingFlags.Static |
								  BindingFlags.Instance;

System.Reflection.MethodInfo privateTestMethod = typeof(MethodInfoClass).GetMethod("PrivateTestMethod", bindingFlags);

if (privateTestMethod is not null)
{
	string returnFromPrivateTestMethod = (string)privateTestMethod.Invoke(mic, null);
	Console.WriteLine($"privateTestMethod: {returnFromPrivateTestMethod}");
}
Example project download from here.

Example of IEnumerable casting

Details
Written by: Stanko Milosev
Category: C#
Published: 29 August 2021
Last Updated: 29 August 2021
Hits: 660
As title said:
using System;
using System.Collections;
using System.Linq;

namespace IEnumarable
{
  class Program
  {
    static void Main(string[] args)
    {
      MyIEnumerable myIEnumerable = new MyIEnumerable();

      foreach (string test in myIEnumerable.Cast<string>().ToList())
      {
        Console.WriteLine(test);
      }
    }

    class MyIEnumerable : IEnumerable
    {
      private string _names = "stanko, milosev, elizabeta, lazarevic";

      public IEnumerator GetEnumerator()
      {
        string[] aryNames = _names.Split();

        foreach (string name in aryNames)
        {
          yield return name;
        }
      }
    }
  }
}
POI:
myIEnumerable.Cast().ToList()

Filter XML nodes per value

Details
Written by: Stanko Milosev
Category: C#
Published: 29 August 2021
Last Updated: 19 September 2021
Hits: 640
  • xml
Small example on how to filter XML per value:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace ConsoleApp15
{
    class Program
    {
        static void Main(string[] args)
        {
            string strXML = @"
                <doc>
	                <ext>Type:A</ext>
	                <ext>Type:B</ext>
                </doc>";

            XElement myXML = XElement.Parse(strXML);
            IEnumerable<XElement> xElementData = from data in myXML.Descendants("ext") where data.Value.Contains("Type:A") select data;
            foreach (XElement xElement in xElementData)
            {
                Console.WriteLine(xElement.Value);
            }

            Console.ReadKey();
        }
    }
}
POI:
IEnumerable xElementData = from data in myXML.Descendants("ext") where data.Value.Contains("Type:A") select data;

Few thread examples

Details
Written by: Stanko Milosev
Category: C#
Published: 29 August 2021
Last Updated: 29 August 2021
Hits: 643
Here is first simple example:
using System;
using System.Threading;

namespace ThreadExample1
{
  class Program
  {
    static void Main(string[] args)
    {
      MyListThreadClass myListThreadClass = new MyListThreadClass();
      Thread backgroundThread = new Thread(new ThreadStart(myListThreadClass.AddToMyList));
      backgroundThread.Name = "Secondary";
      backgroundThread.Start();

      myListThreadClass.AddToMyList();
    }
  }

  public class MyListThreadClass
  {
    public void AddToMyList()
    {
      for (int i = 0; i < 100; i++)
      {
        Console.WriteLine($"i: {i},  Thread.CurrentThread.ManagedThreadId: {Thread.CurrentThread.ManagedThreadId}");
      }
    }
  }
}
First example with parameters:
using System;
using System.Threading;

namespace ThreadExampleWithParameters
{
  class Program
  {
    static void Main(string[] args)
    {
      MyListThreadClass myListThreadClass = new MyListThreadClass();
      Thread backgroundThread = new Thread(new ParameterizedThreadStart(myListThreadClass.AddToMyList));
      backgroundThread.Name = "Secondary";
      backgroundThread.Start("milosev");

      myListThreadClass.AddToMyList("stanko");
    }
  }

  public class MyListThreadClass
  {
    public void AddToMyList(object myName)
    {
      for (int i = 0; i < 100; i++)
      {
        Console.WriteLine($"i: {myName}{i},  Thread.CurrentThread.ManagedThreadId: {Thread.CurrentThread.ManagedThreadId}");
      }
    }
  }
}
Here notice line:

myListThreadClass.AddToMyList("stanko");

and method signature:

public void AddToMyList(object myName)

object

Second example with parameters:

using System;
using System.Threading;

namespace ThreadExampleWithParameters
{
  class Program
  {
    static void Main(string[] args)
    {
      MyListThreadClass myListThreadClass = new MyListThreadClass();
      Thread backgroundThread = new Thread(delegate() { myListThreadClass.AddToMyList("milosev"); });
      backgroundThread.Name = "Secondary";
      backgroundThread.Start();

      myListThreadClass.AddToMyList("stanko");
    }
  }

  public class MyListThreadClass
  {
    public void AddToMyList(string myName)
    {
      for (int i = 0; i < 100; i++)
      {
        Console.WriteLine($"i: {myName}{i},  Thread.CurrentThread.ManagedThreadId: {Thread.CurrentThread.ManagedThreadId}");
      }
    }
  }
}
Here notice how I define thread:

Thread backgroundThread = new Thread(delegate() { myListThreadClass.AddToMyList("milosev"); });

and method signature:

public void AddToMyList(string myName)

string

In next example, every time when something is added to list, I want to notify a method, where I will display new item added to list:

#nullable enable
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Threading;

namespace ThreadExampleWithObservable
{
  class Program
  {
    static void Main(string[] args)
    {

      MyListThreadClass<List<string>> myListThreadClass = new MyListThreadClass<List<string>>();
      ObservableCollection<List<string>> myListThread = new ObservableCollection<List<string>>();
      myListThread.CollectionChanged += myListThreadClass.ReadFromList;
      Thread backgroundWriteThread = new Thread(myListThreadClass.AddToMyList)
      {
        Name = "Secondary"
      };
      backgroundWriteThread.Start(myListThread);

      myListThreadClass.AddToMyList(myListThread);
    }
  }

  public class MyListThreadClass<T> where T : List<string>
  {
    int _callTimes = 0;

    public void AddToMyList(object myListThread)
    {
      for (int i = 0; i < 100; i++)
      {
        ((ObservableCollection<List<string>>)myListThread).Add(new List<string> { $"test.AddToMyList: {i}" });
      }
    }

    public void ReadFromList(object? sender, NotifyCollectionChangedEventArgs e)
    {
      _callTimes++;
      if (e.NewItems != null)
      {
        Console.WriteLine($"e.NewItems.Count: {e.NewItems.Count}");
        foreach (List<string> myItem in e.NewItems)
        {
          Console.WriteLine($"myItem.Count: {myItem.Count}");
          foreach (string itemString in myItem)
          {
            Console.WriteLine(
              $"itemString: {itemString},  callTimes: {_callTimes}, Thread.CurrentThread.ManagedThreadId: {Thread.CurrentThread.ManagedThreadId}");
          }
        }
      }
    }
  }
}
Here notice line:

public void AddToMyList(object myListThread)

as well as that method signature is not type safe.

  1. Check if GPS position is within radius
  2. Reverse geocoding
  3. Change output path without adding target framework
  4. Entity framework Core MySql scaffolding example

Subcategories

C#

Azure

ASP.NET

JavaScript

Software Development Philosophy

MS SQL

IBM WebSphere MQ

MySQL

Joomla

Delphi

PHP

Windows

Life

Lazarus

Downloads

Android

CSS

Chrome

HTML

Linux

Eclipse

Page 3 of 137

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10