- Details
- Written by: Stanko Milosev
- Category: C#
- Hits: 1827
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.
- Details
- Written by: Stanko Milosev
- Category: C#
- Hits: 1838
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
- Details
- Written by: Stanko Milosev
- Category: C#
- Hits: 1732
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
- Details
- Written by: Stanko Milosev
- Category: C#
- Hits: 2509
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.