I cannot find a .net core equivalent for calling GetCustomAttributes on an assembly object. I see there is CustomAttributes property, but this doesn't return instances of the custom attributes, but rather metadata about the attributes. How can I retrieve the actual attribute instance?
There's a GetCustomAttributes extension method. Just add using System.Reflection.
Related
I currently use Json.Net throughout my project and it works like a charm. Unfortunately, the input binding for Azure Functions use System.Text.Json to deserialize the EventGrid event json and bind it toEventGridEvent object from the Azure.Messaging.EventGrid 4.0.0.-beta.4 library
have a look here: https://github.com/Azure/azure-functions-host/issues/5469
This causes massive problems as all my custom JsonConverters that i have do not work at all which leads to incorrectly deserialized objects. Changing from Newtonsoft.Json to System.Text.Json is a massive amount of rework and i would absolutely like to avoid that, if i can.
Is there a way to force the input bindings to use Json.Net instead of System.Text.Jsoneither via DI in FunctionssStartup.Configure() or otherwise?
I'm trying to write a Framework like Spring MVC.
I'm looking for a way to modify the code of the doPost/doGet method of a servlet 3.0 deployed on tomcat using javassist or reflection or whatever so the doPost can call a service method dynamically defined .
doPost(...){
ServiceClassName.methodeName(); // dynamic line of code
}
Thanks
It is possible using javassist. Refer this to write your Transformer class.
Although I have never tried it before, have a look at http://www.bytebuddy.net for a bytecode creation/manipulation library.
I would like to customize at runtime the attributes that MVC sees on a view model property. As far as I know, MVC relies internally on type descriptors to enumerate the attributes. Is there a way to hook a type descriptor somewhere to return a custom list of attributes for a property?
Is there a way to hook a type descriptor somewhere to return a custom
list of attributes for a property?
It depends. If you want to override the Data Annotations used by the metadata provider then you could write your own custom ModelMetadataProvider and replace the default one (DataAnnotationsModelMetadataProvider). This allows you to have a custom metadata provider for a given type and return this information at runtime.
If on the other hand you are doing validation, then you are a bit out of luck. For more flexibility I would recommend you using FluentValidation.NET instead of data annotations.
We're using Moq and I was wondering what the current role of virtual methods were in it-- in the post below dated 2008 it's clear you had to mark your methods as virtual in order for Moq to work (or inherit from an interface.)
Moq discussion
However, is this still the case in .Net 4.5, that you're required to use virutal methods in the class you want to mock? And does this also hold true when you create a wrapper class around a static method-- the wrapper class either inherits from an interface or the method in question needs to be marked virtual?
This hasn't changed in .NET 4 or 4.5. As the link you provided explains, Moq uses Castle Windsor Dynamic Proxy to generate a type derived from the type you wish to mock. Therefore, the standard rules of inheritance apply. The derived type generated by Moq can only intercept calls to methods that any normal derived class can override.
I am new to the structure map but i want to use it in my asp.net site for dependency injection
can any one suggest me simple example to use structure map for the dependency injection
you will need to do something like this:-
StructureMapConfiguration
.ForRequestedType<IResourceA>()
.TheDefaultIsConcreteType<ResourceB>()
.CacheBy(InstanceScope.Singleton);
This tells StructureMap to inject ResourceB when there is a request for ResourceA.
Structure Map
You can configure programatically or via configuration file.
Programatical example (there are other ways):
StructureMap.StructureMapConfiguration.ForRequestedType<ISomething>().TheDefaultIsConcreteType<ConcreteSomething>();
then you can get an instance of the configured type using this sort of code:
//The concrete type will be ConcreteSomething
ISomething instance = ObjectFactory.GetInstance<ISomething>();
You can do it in a config file:
<StructureMap MementoStyle="Attribute">
<DefaultInstance PluginType="Blah.ISomething, Blah.SomethingDLL" PluggedType="Blah.Concrete.ConcreteSomething,Blah.ConcreteDLL"/>
</StructureMap>
and in the main method or Global.asax you can set this config by saying:
StructureMap.ObjectFactory.Initialize(x => { x.PullConfigurationFromAppConfig = true; });
and use it the same way as above:
ISomething instance = ObjectFactory.GetInstance<ISomething>();
If the concrete class has a constructor that needs instances injected in it, and you have those configured, the concrete types will get injected by the framework.
There are ways of passing parameters to constructors, dealing with Gereric types, creating named instances that are configured with specific constructor/property values. I use this framework and like it very much.