JetBrains Rider - how to execute a C# scratch - jetbrains-rider

In JetBrains Rider, when I create a scratch C# file, how can I execute it?
Making the Main method public static and adding a namespace seems to be not enough.
using System;
namespace MyNamespace
{
public class Foo
{
public static void Main()
{
Console.WriteLine("hello");
}
}
}
Rider version: JetBrains Rider 2022.3.1
Windows 10

Related

Visual Studio not resolving Dapper.Extension Namespace

I am trying to learn how to use Dapper.Extension but after multiple attempts installing and reinstalling the nuget package. I can't get the namespace to resolve and usable. I am creating a generic repository but when i attempt to include the namespace, VS doesn't even see it. I have looked all over their documentation and install guides but can't see anyone else having this issue. Is there something stupid i am over looking? See my Find method below using the extension.
public T Find(int id)
{
using(var conn = _ConnectionFactory.GetConnection())
{
return conn.Get<T>(id);
}
}
You have added the libraries correctly but you need to add using references at the top of your classes to import the dapper namespace and add the additional methods to the SqlConnection object. To do so add both of the lines below to the top of your class files, before any namespace declarations.
using Dapper;
using DapperExtensions;
The equivalent in VB.NET would be:
Imports Dapper
Imports DapperExtensions
There are two nuget packages "DapperExtensions" and "Dapper.Extensions". Make sure you have installed the first one. I did this mistake too
To test I used VS 2015:
Create new console application
Install-Package DapperExtensions
Install latest version of Dapper: Install-Package Dapper -version 1.50.2
Here is the test code:
using System.Data.SqlClient;
using DapperExtensions;
namespace ConsoleApplication2
{
public class Foo
{
public int Id { get; set; }
}
class Program
{
static void Main(string[] args)
{
var result = Find(1);
}
public static Foo Find(int id)
{
using (var conn = new SqlConnection(#"Data Source=.\sqlexpress;Integrated Security=true; Initial Catalog=foo"))
{
conn.Open();
var foo = conn.Get<Foo>(id);
return foo;
}
}
}
}
I had a similar issue but that was because I had installed "Dapper.Extension" via nuget instead of installing "DapperExtensions". Could you have done something similar?

Xamarin Cross Platform Application Package Name in C#

Is there a way to programmatically access the application package name/namespace/version information in Xamarin portable C# code?
Like this (link is how to access for Android, I want to know if there is a pre-existing C# cross platform way to access this in the portable class code.)
Not this.
i got answer to your problem, but it is using dependency service to work. There is no other way if there is no Xamarin plugin.
Here is the code:
Interface in pcl project:
public interface IPackageName
{
string PackageName { get; }
}
Android implementation:
public class PackageNameDroid : IPackageName
{
public PackageNameDroid()
{
}
public string PackageName
{
get { return Application.Context.PackageName; }
}
}
iOS implementation:
public class PackageNameIOS : IPackageName
{
public PackageNameIOS()
{
}
public string PackageName
{
get { return NSBundle.MainBundle.BundleIdentifier; }
}
}
Don't forget to mark platform files for dependency service in Android file:
[assembly: Xamarin.Forms.Dependency(typeof(PackageNameDroid))]
and iOS file:
[assembly: Xamarin.Forms.Dependency(typeof(PackageNameIOS))]
Now you're good to go and can use it in PCLs just like that:
string packageName = DependencyService.Get<IPackageName>().PackageName;
Hope it helps others.

Does Subject.Subscribe only work in a static instance (or am I missing something)

I'm mucking about with reactive extensions and Iv'e hit a snag that I can't for the life of me work out what the cause is.
If I use a .NET 4 console mode app, where everything is static as follows:
using System;
using System.Reactive.Subjects;
using FakeDal;
using FakeDal.Entites;
using RxProducer;
namespace Runner
{
class Program
{
private static readonly Subject<DaftFrog> _subject = new Subject<DaftFrog>();
private static readonly Repository<DaftFrog> _frogRepo = new Repository<DaftFrog>();
static void Main()
{
_subject.Subscribe(RespondToNewData);
}
private static void RespondToNewData(DaftFrog frog)
{
_frogRepo.Save(frog);
}
}
}
DaftFrog is just a test class in my fake DAL class, this is a simple .NET 4 Class library project, the DaftFrog class, is a simple poco with a few fields in, the dal.save method just simply does a console.WriteLine of a field in the DaftFrog object.
Both classes are just simple stand in's for the real things once I get around to making the RX code work.
Anyway, back to the problem, so the code above works fine, and if I do a few
_subject.OnNext(new DaftFrog());
calls, the fake dal class, prints out what I expect and everything works fine...
HOWEVER>....
If I then transport this code as is, to a class library, and then new up that class library from within my "static program" as follows:
using System.Reactive.Subjects;
using FakeDal;
using FakeDal.Entites;
namespace RxProducer
{
public class Producer
{
private readonly Subject<DaftFrog> _subject = new Subject<DaftFrog>();
private readonly Repository<DaftFrog> _frogRepo = new Repository<DaftFrog>();
private int _clock;
public void Start()
{
_subject.Subscribe(RespondToNewData);
}
public void Stop()
{
}
public void Tick()
{
if(_clock % 5 == 0)
{
DaftFrog data = new DaftFrog();
_subject.OnNext(data);
}
_clock++;
}
private void RespondToNewData(DaftFrog frog)
{
_frogRepo.Save(frog);
}
}
}
And then use that class in my program
using System;
using RxProducer;
namespace Runner
{
class Program
{
private static readonly Producer _myProducer = new Producer();
static void Main()
{
_myProducer.Start();
while(!line.Contains("quit"))
{
_myProducer.Tick();
line = Console.ReadLine();
}
_myProducer.Stop();
}
}
}
Then my project fails to compile.
Specifically it fails on the line:
_subject.Subscribe(RespondToNewData);
in the RxProducer class library, mores the point, the error the compiler throws back makes little sense either:
Error 1 The best overloaded method match for 'System.Reactive.Subjects.Subject<FakeDal.Entites.DaftFrog>.Subscribe(System.IObserver<FakeDal.Entites.DaftFrog>)' has some invalid arguments H:\programming\rxtesting\RxProducer\Producer.cs 17 7 RxProducer
Error 2 Argument 1: cannot convert from 'method group' to 'System.IObserver<FakeDal.Entites.DaftFrog>' H:\programming\rxtesting\RxProducer\Producer.cs 17 26 RxProducer
At first I thought that it might have been the static thing, so I made everything in the class library static, and that made no difference at all.
Iv'e really not done much with Rx until now, but I work with C# and VS 99% of the time, so I'm aware that the error is telling me it can't convert a type of some description, I just don't understand why it's telling me that, esp when the code works perfectly in the static program, but not in a class library.
Shawty
UPDATE
Second thoughts, I just know there are going to be those who insist that I post the fakedal and daft frog definitions, even though IMHO they won't be required, but to pacify the hordes of pretenders who will ask here they are :-)
using System;
namespace FakeDal
{
public class Repository<T>
{
public void Save(T entity)
{
Console.WriteLine("Here we write T to the database....");
}
}
}
namespace FakeDal.Entites
{
public class DaftFrog
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsTotalyDaft { get; set; }
}
}
Include using System; into file where you have Producer, this will help to convert RespondToNewData to IObserver<T>.
Sounds like the compiler is having trouble inferring the Action...might be missing a using statement for the relevant extension method. Alternatively, try either of:
_subject.Subscribe ((Action<DaftFrog>) RespondToData);
Or:
var obs = Observer.Create ( I forget the overload );
_subject.Subscribe( obs);

When using Visual Studio custom templates with parameters, how can you get the actual root namespace?

I am attempting to set up a template to create a unit test class with some basic structure and default using statements. I have created a VS2010 custom template to do this and almost everything works correctly.
The issue I have encountered is that I am attempting to reference my project's base namespace in my using statements, and am unable to determine whether there is a parameter that will let me do this.
An example of what I want, assuming my project is called MyProduct.Test:
using NUnit.Framework;
using MyProduct.Test.Base;
namespace MyProduct.Test.BLL.Services.MyService
{
[TestFixture]
public class MyServiceTest : TestBase
{
[SetUp]
public void Setup()
etc...
I can generate everything except for using MyProduct.Test.Base with the template below:
using NUnit.Framework;
using $safeprojectname$.Base;
namespace $rootnamespace$
{
[TestFixture]
public class $safeitemname$ : TestBase
{
[SetUp]
public void Setup()
etc...
Unfortunately, $safeprojectname$ is only available for Project templates, not Item templates.
This template results in the following output:
using NUnit.Framework;
using $safeprojectname$.Base;
namespace MyProduct.Test.BLL.Services.MyService
{
[TestFixture]
public class MyServiceTest : TestBase
{
[SetUp]
public void Setup()
etc...
Is there any way for me to get the actual root namespace of my project (since $rootnamespace$ actually outputs the namespace for the file being added based on its location)?
Note: Obviously I can set this up to work fine for one project, but it would be much more useful to have a template that works for all projects.

TypeInitializationException When Using Moles With HtmlAgilityPack

I am attempting to use Moles to test a non-static method in a separate assembly. When running the test without the [HostType("Moles")] tag, the test runs fine. When I replace it I receive the following error:
"The type initializer for 'HtmlAgilityPack.HtmlNode' threw an exception."
I have attached code samples that perform in an identical manner.
Any help would be great!
Class/method being called by the unit test
using System;
using HtmlAgilityPack;
using System.Web;
namespace HAPAndMoles
{
public class Class1
{
public void fooBar()
{
HtmlDocument foo = new HtmlDocument();
}
}
}
Unit Test
using System;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using HAPAndMoles;
using Microsoft.Moles.Framework;
using HtmlAgilityPack;
using System.Web;
namespace HAPAndMoles
{
[TestClass]
public class UnitTest1
{
[TestMethod]
[HostType("Moles")]
public void TestMethod1()
{
Class1 bar = new Class1();
bar.fooBar();
}
}
}
I'm not sure I understand your example because in fact you don't use Moles.
If you just want to "Mole" our own non-virtual method, in the references of your test project you just have to right-click on the assembly of the tested project and choose Add Moles Assembly. That will create an HAPAndMoles.Moles reference.
Then add the corresponding using and you can call your class "moled" starting with M (Class1 => MCLass1). I show you an example testing the MClass1 behaviour:
using Microsoft.VisualStudio.TestTools.UnitTesting;
using HAPAndMoles;
using HAPAndMoles.Moles;
namespace HAPAndMoles {
[TestClass]
public class UnitTest1
{
[TestMethod]
[HostType("Moles")]
public void TestMethod1()
{
bool called = false;
var bar = new MClass1()
{
fooBar = () => called = true
};
((Class1)bar).fooBar();
Assert.IsTrue(called);
}
}
}
When I want Moles of mscorlib, I right-click directly on the references of the test project and I can Add Moles Assembly for mscorlib. Then the
using Microsoft.Moles.Framework;
is needed.

Resources