Page 146 - CSharp/C#
P. 146
And the invocation looks like this:
public void Save(DBContext context)
{
try
{
context.SaveChanges();
}
catch (Exception ex)
{
LogException(ex);
}
}
Notice that only the first parameter is passed explicitly to the LogException method whereas the rest
of them will be provided at compile time with the relevant values.
The callerMemberName parameter will receive the value "Save" - the name of the calling method.
The callerLineNumber parameter will receive the number of whichever line the LogException method
call is written on.
And the 'callerFilePath' parameter will receive the full path of the file Save method is declared in.
Reading an attribute from interface
There is no simple way to obtain attributes from an interface, since classes does not inherit
attributes from an interface. Whenever implementing an interface or overriding members in a
derived class, you need to re-declare the attributes. So in the example below output would be True
in all three cases.
using System;
using System.Linq;
using System.Reflection;
namespace InterfaceAttributesDemo {
[AttributeUsage(AttributeTargets.Interface, Inherited = true)]
class MyCustomAttribute : Attribute {
public string Text { get; set; }
}
[MyCustomAttribute(Text = "Hello from interface attribute")]
interface IMyClass {
void MyMethod();
}
class MyClass : IMyClass {
public void MyMethod() { }
}
public class Program {
public static void Main(string[] args) {
GetInterfaceAttributeDemo();
}
https://riptutorial.com/ 92

