Dynamic parameters in postsharp - c#

I am trying to assign the property dynamically using post sharp. Such as in the below example I would like to have custom logging messages. But the problem is how could I set xname in the attribute part.In the first log I would like to have the logging message would have the persons name in the beginning but the second logging message would have the persons name at the end.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplicationLogging
{
internal static class Program
{
private static void Main()
{
Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
SayHelloInTurkish("Ayse");
SayGoodByeSayHelloInEnglish("Elizabeth");
}
[Trace("xname Loglanıyor Gunaydın")]
private static void SayHelloInTurkish(string personName)
{
Console.WriteLine("Hello, world.");
}
[Trace("Good Bye Logging for xname")]
private static void SayGoodByeSayHelloInEnglish(string personName)
{
Console.WriteLine("Good bye, world.");
}
}
}
using System;
using System.Diagnostics;
using PostSharp.Aspects;
namespace ConsoleApplicationLogging
{
[Serializable]
public sealed class TraceAttribute : OnMethodBoundaryAspect
{
private readonly string category;
public TraceAttribute(string category)
{
this.category = category;
}
public string Category { get { return category; } }
public override void OnEntry(MethodExecutionArgs args)
{
if (args.Method.DeclaringType != null)
Trace.WriteLine(string.Format("Entering {0}.{1}.",
args.Method.DeclaringType.Name, args.Method.Name), this.category);
}
public override void OnExit(MethodExecutionArgs args)
{
if (args.Method.DeclaringType != null)
Trace.WriteLine(string.Format("Leaving {0}.{1}.",
args.Method.DeclaringType.Name, args.Method.Name), this.category);
}
}
}

If I understand correctly, you need something like this (no sanity checking included):
this.category.Replace("xname", (string)args.Arguments[0])
If I did not understand correctly, feel free to comment below.

Related

How to call a class rather than a method inside a class?

so I want to make it easy to call Console.WriteLine() (from the namespace system) so I wrote:
using System;
namespace main
{
namespace easier
{
public class print
{
public print(string input)
{
Console.WriteLine(input);
}
}
}
}
But when I try to call print("Hello World); it says CS1955: Non-invocable member 'print' cannot be used like a method.
Is there a way so I don't have to do class.print("Hello World"); just print("Hello World");?
Thanks in advance!
You can use using static
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-static
using System;
using static Easier;
class Program
{
static void Main(string[] args)
{
Print("message");
}
}
public static class Easier
{
public static void Print(string value)
{
Console.WriteLine(value);
}
}

How do I start an Activity from a non-Activity class?

How can I start an Activity from a non-Activity in Xamarin.Forms?
In the main startup project, MyApp.Droid, I have MyActivity.cs.
In the PCL project, MyApp.Plugin.Android, I have a non-activity class, MyClass.cs.
In MyClass.cs I need to run some code in MyActivity.cs.
How can I do that?
Thanks.
MyActivity.cs:
public void MyMethod()
{
// [Working Code...]
}
MyClass.cs:
public void OnSuccess
{
// What should I put here to call MyMethod?
}
something like this should do the trick! (I think, it been a while...)
MyClass.cs:
using System;
namespace MyClass {
public static class Program {
public static void MyMethod()
{
// [Working Code...]
}
}
}
MyActivity.cs:
using System;
using MyClass; //get your other file by the namespace
namespace MyActivity {
public static class Program {
public static void OnSuccess()
{
Program.MyMethod(); //[class name]..MyMethod();
}
}
}

error CS0116: A namespace cannot directly contain members such as fields or methods- hand writing coding using notepad

Getting the error on line 16, with the foreach. My professor wont email fast enough and the due date is in a few hours! I think I am missing a list or something. I think the d after the double is incorrect. Any help is appectiated!
using System;
using System.Windows.Forms;
using System.Collections.Generic;
public class Starbucks
{
public static void Main()
{
double[] x = {4.2, 5.7, 3.6, 9.1, 2.7, 8.4 };
}
}
static void MyGenerics(double x)
{
foreach (double d in x)
{
MessageBox.Show(x);
}
}
That's because you have defined the MyGenerics() method outside the class Starbucks. Move it inside the class. Error message is exactly telling the same thing. Your code should look like
using System;
using System.Windows.Forms;
using System.Collections.Generic;
public class Starbucks
{
public static void Main()
{
double[] x = {4.2, 5.7, 3.6, 9.1, 2.7, 8.4 };
MyGenerics(x);
}
static void MyGenerics(double[] xx)
{
foreach (double d in xx)
{
MessageBox.Show(d);
}
}
}
This problem can be caused by bad indentation. For example:
using System;
namespace Interrogacion_1.Model
{
interface IClass
{
public string Name { get; set; }
}
{
Console.WriteLine("HELLO WORLD"); #here error cs0116
}
}
The correct way would be:
using System;
namespace Interrogacion_1.Model
{
interface IClass
{
public string Name { get; set; }
public void Imprimir()
{
Console.WriteLine("HELLO WORLD");
}
}
}

Selenium Nunit addin install exception

I don't have enough rep to comment, so I am posting a question here. I read this question Get list of failing tests from Nunit. I am trying to implement the nunit addin, I used this code:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using NUnit.Core.Extensibility;
namespace NunitAddin
{
[NUnitAddinAttribute(Type = ExtensionType.Core,
Name = "addin",
Description = "addin")]
public class NunitAddin : IAddin
{
public bool Install(IExtensionHost host)
{
IExtensionPoint listeners = host.GetExtensionPoint("EventListeners");
if (listeners == null)
return false;
listeners.Install(this);
return true;
}
public void TestStarted(NUnit.Core.TestName testName)
{
}
public void TestFinished(NUnit.Core.TestResult result)
{
}
public void RunStarted(NUnit.Core.TestName testName)
{
}
public void RunFinished(NUnit.Core.TestResult result)
{
}
public void UnhandledException(Exception exception)
{
}
public void TestOutput(NUnit.Core.TestOutput testOutput)
{
}
}
}
But when I call it using
var addin = new NunitAddin.NunitAddin();
var a = addin.Install(CoreExtensions.Host);
I get an error
NunitAddin.NunitAddin is not {0} extension point
on
listeners.Install(this);
Does anyone know how to solve this problem?
Never mind, issue solved. Just a stupid mistake, I had NunitAddin : IAddin instead of NunitAddin : IAddin; EventListener

Nunit is not running in any test method

I have two classes in class library
namespace ClassLibrary3
{
public class Class1
{
public string title;
public string author;
public Class1(string title, string author)
{
this.title = title;
this.author = author;
}
}
}
Another class
using System;
using System.Collections;
using System.Linq;
using System.Text;
namespace ClassLibrary3
{
class Class2
{
private Hashtable books;
public Class2()
{
books = new Hashtable();
}
public void addBook(Class1 book)
{
books.Add(book.title, book);
}
public Class1 getBook(String title, String author)
{
return (Class1)books[title];
}
public void removeBook(string title)
{
if (books[title] != null)
books.Remove(title);
}
}
}
And my test is
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using System.Collections;
namespace ClassLibrary3
{
[TestFixture]
class TEST
{
[Test]
public void getbooktest()
{
Class1 c1 = new Class1("story", "James");
Class2 c2 = new Class2();
Assert.AreEqual("story", c2.getBook("story", "James"));
}
}
}
Basicly the problem is Nunit doesnt test it. It finds the dll. Loads the test class. But dont come upto the test method.
Please any idea..........
NUnit can't see your TEST class unless you mark it as public, change it to
[TestFixture]
public class TEST
{
...
Side note, consider giving it a better name than TEST ;-)

Categories

Resources