Pass method as Parameter using C# 2.x (not 3.x) - c#-2.0

It's 2013, and a major platform coughUnity3dcough is seemingly still partly stuck on 2.x (latest Unity is newer, but isn't production-quality yet for some features). It seems there's a lot of stuff that was missing from the language until 3.5 - e.g. Func and Action - so I'm trying to find a way to do this without using 3.x.
Here's a great, very neat solution for 3.x: Pass Method as Parameter using C#
I tried Microsoft's site, but it's currently refusing to give out docs for anything earlier than 4.5 (was working a few weeks ago, but now their Javascript always errors out :( ).
...what's the equivalent in 2.x? Or is there no way of doing this (sad face) ?
OR: am I missing something obvious about using Func? I get compiler errors that it's an undefined symbol, and I'm using the same context as I found in all the different online resources / examples :(

It's important to distinguish between language versions and platform versions... and likewise functionality. Currently you're mixing up various aspects.
Func and Action aren't part of the C# language at all - they're just delegate types which are part of the .NET framework. If you're using a version of the framework which doesn't include the delegate types you want, just declare your own. I'd suggest naming them differently to the .NET ones so that it won't matter if you then upgrade - there won't be any naming collisions. So:
public delegate void MyAction();
public delegate void MyAction<T>(T input);
... etc
Then once the relevant types are in place, you can use method group conversions as normal:
public void DoSomething(MyAction<string> action) { ... }
public void Foo(string text) { ... }
...
DoSomething(Foo);

Use delegates! Here are some links
http://msdn.microsoft.com/en-us/library/ms173171(v=vs.90).aspx
http://msdn.microsoft.com/en-us/library/aa288459(v=vs.71).aspx

Related

Creating swagger example models asp.net core web api

I do not know how to add the example models with the latest swagger (swashbuckle.aspnetcore4.0.1) NuGet package.
earlier I used to do with this, but i donot get argument with the latest interface
public class SwaggerExamplesSchemaFilter : ISchemaFilter
{
public void Apply(Schema schema, SchemaRegistry schemaRegistry, Type type)
{}
}
I need the latest usage to create the example model
Unless I've missed an update to Swashbuckle, the sort of official unofficial way to add examples is via the Swashbuckle.AspNetCore.Examples NuGet package. I'm sure you could roll your own, if you really wanted to, but you'd likely just create something like this anyways. In other words, there's nothing I'm aware directly built-in to add examples.
I found the answer here for the latest package
https://github.com/domaindrivendev/Swashbuckle.AspNetCore#enrich-schema-metadata

Razor Engine not working in ASP.net 5 MVC 6

I am trying to migrate some existing code from MVC5 to MVC6 and I am having difficulty with this particular code:
Engine.Razor.RunCompile(File.ReadAllText(emailTemplatePath), "emailTemplateKey", typeof (EmailViewModel), emailViewModel);
I am receiving the following runtime error:
MissingMethodException: Method not found: "Void Microsoft.AspNet.Razor.CodeGenerators.GeneratedClassContext.set_ResolveUrlMethodName(System.String)". in RazorEngine.Compilation.CompilerServiceBase.CreateHost(Type templateType, Type modelType, String className)
The original code I was using in MVC5 was taken from here. If there is no way of converting the above code to work with MVC6 what is another elegant way of doing email templates?
Apparently there has been a change in GeneratedClassContext class - the property ResolveUrlMethodName does not exist anymore, hence the MissingMethodException. Looks like ParserContext class has changed too, since accessing OnError event handler throws the same exception.
In fact it is the setter of the missing property missing (pardon the expression!), which, being a method, causes the exception. Absolutely accurate but somewhat misleading, unless you recall this.
Quite a similar question (and a good answer with alternative solution!) here: RazorEngine and MVC 6 beta 7.

Using Kotlin with Dagger

What's the best way to use Dagger to inject dependencies into classes, especially zero-arg constructor classes like Activities, with Dagger? Will Dagger 2 possibly bring improvements to the situation?
Thanks in advance.
Since Kotlin M13 release, a new property has been especially added in order to support dependency injection (like with Dagger 1&2) and other frameworks.
It's called lateinit property. Taken from the documentation:
class Example {
#Inject
lateinit var bar: Bar
}
In a nutshell, bar has no initializers but is declared as a non-null type. If you try to read it before the initialization, an exception is thrown.
Otherwise, once it's initialized using Dagger, it can be used as a normal property.
Everything is well explained in the language doc and you can also check the blog post relative to the M13 release there.
I wasn't updating my answer for a while and it got outdated. Also noticed here new answer from #Ben that works for M13/M14.
Decided it would best if I redirect all of you interested to my template project which I try to keep up to date with latest Kotlin and Dagger 2 versios. Kotlin + Dagger 2 sample
I am describing there how to inject objects, including multiple annotations etc.
Dagger relies on annotation processing, which is not supported yet in Kotlin, as far as I know. But they say, it is planned. And while, you can either extend java classes with Dagger dependencies, or try some reflection based injection framework - Guice, Roboguice
for using dagger annotations in Kotlin
1- // dagger add dependencies in app level
implementation 'com.google.dagger:dagger:2.38.1'
kapt 'com.google.dagger:dagger-compiler:2.38.1'
add in app level/gradle
plugins {
id 'kotlin-kapt'
}
now you can use annotations like #Inject #Component etc
class Test{
#Inject
lateinit var name: String
}

Ninject 3 multiple bindings

My question is really a repeat of an old question posted here:
Ninject 2.2 multiple bindings
It seems someone was going to deal with this back in 2011. Does anyone know if there is some way to turn off such warnings in Ninject? Or some other workaround?
EDIT
In response to #BatteryBackupUnit, here is my exact problem:
I have multiple libraries... and in my core library, I do something like this:
Find all assemblies referenced by the host application (including the host)
Find all types inheriting from IDependency from all those assemblies.
Automatically register all of those as transient
Then from another library (which may or may not be referenced by the host app), I have this:
Kernel.Bind<IDbContextFactory>().To<DbContextFactory>().InSingletonScope();
Here IDbContextFactory is also an IDependency, so it got loaded already by the core library and now I register it here but with a different scope (singleton).
From experience (and having tested it earlier) I know this is no problem in Autofac, but Ninject gives me that error message about having already registered it.
Ideally it would be better to just override any previous registrations... "cascade style" (for lack of a better phrase)..
Ninject does now support overriding open generic bindings with more specific ones.
For Example:
public interface IFoo<T> { }
public class Foo<T> : IFoo<T> { }
public class StringFoo : IFoo<string> {}
used like:
var kernel = new StandardKernel();
kernel.Bind(typeof(IFoo<>)).To(typeof(Foo<>));
kernel.Bind<IFoo<string>>().To<StringFoo>();
var intFooInstance = kernel.Get<IFoo<int>>();
var stringFooinstance = kernel.Get<IFoo<string>>();
Works.
However, if you're not talking about open generic bindings, ninject 3 still handles multi bindings the same as ninject 2.2.
In most scenarios you can work around this by using contextual bindings. Okay i would not exactly call it a workaround, i would call it good design.
In general this is described here: https://github.com/ninject/ninject/wiki/Contextual-Binding
A simple way would be to specify the binding using a name. This requires one binding for the specified one and allows only one, too.
See: https://github.com/ninject/ninject/wiki/Contextual-Binding#simple-constrained-resolution-named-bindings
It is also possible to define a "default" binding like .Bind<IFoo>().To<Foo>(); and special case bindings with the .When(...) syntax, like:
.Bind<IFoo>().To<SpecialFoo>().When(ctx => ...)
See https://github.com/ninject/ninject/wiki/Contextual-Binding#specifying-constraints-on-the-type-binding-using-arbitrary-elements-of-the-resolution-request-context
If you show us your concrete problem we might be able to provide a more concrete solution.

cannot resolve class name 'InvocationMirror' in Dart

I've been trying to use Dart's noSuchMethod(), but this code
noSuchMethod(InvocationMirror invocation) {
// ...
}
results in the following error: cannot resolve class name 'InvocationMirror' from 'MyClass'
It looks like there's no such class as InvocationMirror. Could it be an older version of dart which doesn't have it? The one I have is the stable one from SDK for Mac downloaded just today.
You're observing an inconsistency between specification and implementation (which shouldn't come as a surprise, there are a lot of them). You can use noSuchMethod, at least on Dart VM (don't know about dart2js), but it looks like this: noSuchMethod(String name, List arguments). In the future, when reflection finally comes to Dart, it will change.
You can currently not use the InvocationMirror class as mirror based reflection is not yet implemented in Dart (as mentioned in this article).

Resources