Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Caller information attributes demo
using System;
using System.IO;
using System.Runtime.CompilerServices;
class CallerInfoDemo
{
static void Main(string[] args)
{
LogMe();
Method1();
Console.ReadLine();
}
private static void Method1()
{
LogMe();
}
private static void LogMe(
[CallerFilePath] string sourceFile = "",
[CallerLineNumber] int sourceLineNo = 0,
[CallerMemberName] string memberName = "")
{
Console.WriteLine("Member Name: {0}", memberName);
Console.WriteLine("Line No: {0}", sourceLineNo);
Console.WriteLine("File name: {0}", Path.GetFileName(sourceFile));
Console.WriteLine();
}
}
/*
output:
Member Name: Main
Line No: 9
File name: CallerInfoDemo.cs
Member Name: Method1
Line No: 16
File name: CallerInfoDemo.cs
*/
using System;
using System.IO;
using System.Runtime.CompilerServices;
class Demo
{
static void Main(string[] args)
{
var instance = new Demo();
var newinstance = (Demo)3;
Demo newinstance2 = (Demo)"string";
var mystring = newinstance[1];
var temp = instance + instance;
}
static Demo()
{
LogMe("Static constructor");
}
public string this[int id]
{
get
{
LogMe("Indexer");
return "";
}
}
private Demo()
{
LogMe("Constructor");
}
~Demo()
{
LogMe("Finaliser");
}
public static implicit operator Demo(int demoInt)
{
LogMe("Implicit conversion operator");
return new Demo();
}
public static explicit operator Demo(string demoString)
{
LogMe("Explicit conversion operator");
return new Demo();
}
public static Demo operator +(Demo left, Demo right)
{
LogMe("Operator overload");
return left;
}
private static void LogMe(string description,
[CallerMemberName] string memberName = "")
{
File.AppendAllText("output.txt",
"Type: " + description
+ Environment.NewLine
+ "Value is: " + memberName
+ Environment.NewLine
+ Environment.NewLine);
}
}
/*
output:
Type: Static constructor
Value is: .cctor
Type: Constructor
Value is: .ctor
Type: Implicit conversion operator
Value is: op_Implicit
Type: Constructor
Value is: .ctor
Type: Explicit conversion operator
Value is: op_Explicit
Type: Constructor
Value is: .ctor
Type: Indexer
Value is: Item
Type: Operator overload
Value is: op_Addition
Type: Finaliser
Value is: Finalize
Type: Finaliser
Value is: Finalize
Type: Finaliser
Value is: Finalize
*/
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
class PropertyChangedDemo
{
public static void Main(string[] args)
{
var dummy = new NewStyle();
dummy.PropertyChanged += (sender, eventArgs) =>
{
Console.WriteLine("{0} has changed to {1}"
, eventArgs.PropertyName
, dummy.Name);
};
do
{
Console.Write("New name: ");
dummy.Name = Console.ReadLine();
}
while (true);
}
}
class OldStyle : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string _name;
public string Name
{
get { return _name; }
set
{
_name = value;
OnNotifyPropertyChanged("Name");
}
}
private int _age;
public int Age
{
get { return _age; }
set
{
_age = value;
OnNotifyPropertyChanged("Age");
}
}
private void OnNotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
class NewStyle : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string _name;
public string Name
{
get { return _name; }
set
{
_name = value;
OnNotifyPropertyChanged();
}
}
private int _age;
public int Age
{
get { return _age; }
set
{
_age = value;
OnNotifyPropertyChanged("Age");
}
}
private void OnNotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.