logo
down
shadow

C# Ninject binding from Dictionary<Type, Type>


C# Ninject binding from Dictionary<Type, Type>

Content Index :

C# Ninject binding from Dictionary<Type, Type>
Tag : chash , By : user183842
Date : November 23 2020, 03:01 PM

I think the issue was by ths following , Ok your issue is that you are invoking the method on the kernel object and not on the result of the method. This will fix your problem
MethodInfo method = kernel.GetType().GetMethods().FirstOrDefault(x=>x.Name == "Bind");
MethodInfo genericBind = method.MakeGenericMethod(bind.Key);
var result = genericBind.Invoke(kernel,null);
MethodInfo bindResult = result.GetType().GetMethods().FirstOrDefault(x => x.Name == "To" && x.IsGenericMethod == true);
MethodInfo genericTo = bindResult.MakeGenericMethod(bind.Value);
genericTo.Invoke(result, null); //Error is here

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

XAML markup binding to dictionary with key of type Type


Tag : .net , By : Arun Thomas
Date : March 29 2020, 07:55 AM
will be helpful for those in need If the indexer has a specific type the conversion should be done automatically so this should work:
{Binding theDictionary[ns:OnePrettyType]}
{Binding theDictionary[(sys:Type)ns:OnePrettyType]}
{Binding Path=theDictionary[(sys:Type)ns:OnePrettyType]}
[ContentProperty("Parameters")]
public class PathConstructor : MarkupExtension
{
    public string Path { get; set; }
    public IList Parameters { get; set; }

    public PathConstructor()
    {
        Parameters = new List<object>();
    }
    public PathConstructor(string path, object p0)
    {
        Path = path;
        Parameters = new[] { p0 };
    }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return new PropertyPath(Path, Parameters.Cast<object>().ToArray());
    }
}
<Binding>
    <Binding.Path>
        <me:PathConstructor Path="theDictionary[(0)]">
            <x:Type TypeName="ns:OnePrettyType" />
        </me:PathConstructor>
    </Binding.Path>
</Binding>
{Binding Path={me:PathConstructor theDictionary[(0)], {x:Type ns:OnePrettyType}}}

Ninject conditional binding based on parameter type


Tag : chash , By : turret
Date : March 29 2020, 07:55 AM
I hope this helps you . I'm using a factory to return a datasender: , After some looking into Ninject source I have found following:
public class DummyContext : IContext
{
    public IKernel Kernel { get; private set; }
    public IRequest Request { get; private set; }
    public IBinding Binding { get; private set; }
    public IPlan Plan { get; set; }
    public ICollection<IParameter> Parameters { get; private set; }
    public Type[] GenericArguments { get; private set; }
    public bool HasInferredGenericArguments { get; private set; }
    public IProvider GetProvider() { return null; }
    public object GetScope() { return null; }
    public object Resolve() { return null; }
}
kernel.Bind<IDataSender>()
      .To<RemotingDataSender>()
      .When( a => a.Parameters
                   .Single( b => b.Name == "connection" )
                   .GetValue( new DummyContext(), a.Target ) 
               as RemotingConnection != null );

How to check if there is Ninject binding exists by type?


Tag : chash , By : ugufugu
Date : March 29 2020, 07:55 AM
will be helpful for those in need In case of auto-injecting the dependencies, I realized that the object creation is not that bad since it will be consumed by a controller or a service anyway.

Binding multiple versions of the same type with Ninject


Tag : chash , By : Brian Cupps
Date : March 29 2020, 07:55 AM
With these it helps It's perfectly fine to create two bindings for the same type, which differ only in parameters. So what you've got to do is:
Bind<IFoo>().To<Class1>().WithConstructorArgument("boolParameterName", true);
Bind<IFoo>().To<Class1>().WithConstructorArgument("boolParameterName", false);
public interface IBar { }
public class Bar : IBar { }

public interface IFoo { }

class Foo1 : IFoo
{
    public Foo1(IBar bar) { }
}

class Foo2 : IFoo
{
    public Foo2(IBar bar, bool theParametersName) { }
}

    [Fact]
    public void FactMethodName()
    {
        var kernel = new StandardKernel();
        kernel.Bind<IBar>().To<Bar>();
        kernel.Bind<IFoo>().To<Foo1>();
        kernel.Bind<IFoo>().To<Foo2>().WithConstructorArgument("theParametersName", true);
        kernel.Bind<IFoo>().To<Foo2>().WithConstructorArgument("theParametersName", false);

        List<IFoo> foos = kernel.GetAll<IFoo>().ToList();

        foos.Should().HaveCount(3);
    } 

Ninject generic type xml binding


Tag : chash , By : Ben Brown
Date : March 29 2020, 07:55 AM
I wish did fix the issue. You want to bind open generic types, so this type definition should do the trick:
<bind service="Base.IJsonProvider`1, Base" to="Base.JsonProvider`1, Base" 
    name ="Config"/>
Related Posts Related QUESTIONS :
  • sgen.exe fails during build
  • In C#, why can't a List<string> object be stored in a List<object> variable
  • C# loop - break vs. continue
  • How to access .Net element on Master page from a Content page?
  • Why are unsigned int's not CLS compliant?
  • Why is Array.Length an int, and not an uint
  • How do I make event callbacks into my win forms thread safe?
  • Genealogy Tree Control
  • WCF Service - Backward compatibility issue
  • Binary patch-generation in C#
  • Tab Escape Character?
  • When do Request.Params and Request.Form differ?
  • The imported project "C:\Microsoft.CSharp.targets" was not found
  • Numeric Data Entry in WPF
  • Print a Winform/visual element
  • C# logic order and compiler behavior
  • When to use an extension method with lambda over LINQtoObjects to filter a collection?
  • How to make a button appear as if it is pressed?
  • C# and Arrow Keys
  • How do you resolve a domain name to an IP address with .NET/C#?
  • Should the folders in a solution match the namespace?
  • How can I evaluate C# code dynamically?
  • CSharpCodeProvider Compilation Performance
  • How can I create Prototype Methods (like JavaScript) in C#.Net?
  • DataTable Loop Performance Comparison
  • CSV string handling
  • What is the best way to do unit testing for ASP.NET 2.0 web pages?
  • High availability
  • What to use for Messaging with C#
  • Accessing a Dictionary.Keys Key through a numeric index
  • ConfigurationManager.AppSettings Performance Concerns
  • What Are Some Good .NET Profilers?
  • Is this a good way to determine OS Architecture?
  • How to create a tree-view preferences dialog type of interface in C#?
  • Searching directories for tons of files?
  • Can I have a method returning IEnumerator<T> and use it in a foreach loop?
  • Why can't I have abstract static methods in C#?
  • Displaying ad content from Respose.WriteFile()/ Response.ContentType
  • Convert integers to written numbers
  • Absolute path back to web-relative path
  • How can we generate getters and setters in Visual Studio?
  • Bringing Window to the Front in C# using Win32 API
  • Possible to "spin off" several GUI threads? (Not halting the system at Application.Run)
  • Reading a C/C++ data structure in C# from a byte array
  • How should I translate from screen space coordinates to image space coordinates in a WinForms PictureBox?
  • Setting Objects to Null/Nothing after use in .NET
  • Converting ARBG to RGB with alpha blending
  • Is it better to create Model classes or stick with generic database utility class?
  • Passing enum type to Converter with integer value
  • Pool of objects with objects that are already on the scene in advance
  • StatusBar text fade-out when binding using Caliburn.Micro
  • Queryfilter on ApplicationUser in OnModelCreating in ApplicationDbContext creates StackOverflowException
  • How to get record form a different table based on a value from first table with linq expression?
  • Show data in Grid from returned model
  • Using Attributes to Override Data Model Conventions
  • Basic OOP console calculator, result Error
  • Compositon and Repository pattern
  • Multiple using statements with if condition
  • How do i increase a number by 1 in every line that contain the number 1
  • Add binding to elements that are created in codebehind
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com