Xamarin binding call methods - binding

I've created a binding project to my original project, i've added my library to the binding project. This is my code in ApiDefinition.cs:
public partial interface TestInterface1
{
[Static,Export ("sum:with:")]
int TestAdd (int first, int second);
}
I added the binding as a reference to my original project, but how do i call the TestAdd() ? I find the interface in the namespace but im not sure how to use it?
Thanks

You would need to get hold of an instance of an object that implements TestInterface1. If you don't have such a thing you maybe want to change your interface to a class so you can instantiate an object of type TestInterface1 yourself?
public class TestInterface1
{
[Static,Export ("sum:with:")]
int TestAdd (int first, int second);
}
and then use it like
var test = new TestInterface1();
var result = test.TestAdd(1,2);
Makes sense?

Related

Is there a way to access a mixin's private variable in the class using the mixin?

In dart when creating a mixin, you can declare properties and methods like a class. When declaring a private property/method, it seems the inheriting class should also have access to this private member (see below for example).
Is there a way to access a mixin's private variable in the class using the mixin?
If it's not possible, how can I declare a member in the mixin object but make it private in the inheriting class's interface.
mixin.dart
mixin A {
String propertyOne = '1';
// This property is not accessible to any inheriting class.
int _privateProperty = 2;
}
class.dart
class B with A {
String get mixinString => propertyOne;
// This property is not accessible to the B class.
int get mixinInt => _privateProperty;
}
No. A property being library private means that you can only express its name inside the same library. In any other library, the identifier _privateProperty is a different name, one private to that other library.
If you cannot declare both mixin and class in the same library, and you definitely need access to the property, then you can do any number of things to allow that.
Make the property public and tell people not to use it except in subclasses. They still can if they want to.
Make the property public and mark it #protected, to have the analyzer tell people to not use it except in subclasses. They still can if they want to.
Keep the property private and provide a separate method to access it:
mixin A {
// This property is not accessible to any inheriting class.
int _privateProperty = 2;
static int getPrivateProperty(A a) => a._privateProperty;
static void setPrivateProperty(A a, int value) {
a._privateProperty = value;
}
}
Anyone can still get to the property if they really want to, but they need to know that
it comes from A.

How to get the instance of an injected dependency, by its type using Umbraco.Core.Composing (Umbraco 8)

I need to find a way to get an instance of DataProcessingEngine without calling it's constractor.
I am trying to find a way to do so using the registered DataProcessingEngine in composition object (please see the following code). But I could not find a way to do so.
Anyone have a suggestion? Thanks in advance.
public class Composer : IUserComposer
{
public void Compose(Composition composition)
{
composition.Register<IDataProcessingEngine, DataProcessingEngine>(Lifetime.Singleton);
//DataProcessingEngine dataProcessing = compostion.Resolve<IDataProcessingEngine>()??//no resolve function exists in Umbraco.Core.Composing
SaveImagesThread(dataProcessingEngine);
}
public Task SaveImagesThread(IDataProcessingEngine dataProcessingEngine)//TODO - decide async
{
string dataTimerTime = WebConfig.SaveProductsDataTimer;
double time = GetTimeForTimer(dataTimerTime);
if (time > 0)
{
var aTimer = new System.Timers.Timer(time);
aTimer.Elapsed += new ElapsedEventHandler(dataProcessingEngine.SaveImages);
aTimer.Start();
}
return default;
}
}
For all of you who are looking for a way to call a function (that's defined in another class in your code, an Engine or ...) from the composer(where the app starts) and want to avoid calling this function's class' constractor. I've found another way to do so:
public class QueuePollingHandler
{
[RuntimeLevel(MinLevel = RuntimeLevel.Run)]
public class SubscribeToQueuePollingHandlerComponentComposer :
ComponentComposer<SubscribeToQueuePollingHandler>
{ }
public class SubscribeToQueuePollingHandler : IComponent
{
private readonly IDataProcessingEngine _dataProcessingEngine;
public SubscribeToQueuePollingHandler(IDataProcessingEngine
dataProcessingEngine)
{
_dataProcessingEngine = dataProcessingEngine;
SaveImagesThread(_dataProcessingEngine);
}
public void SaveImagesThread(IDataProcessingEngine
dataProcessingEngine)
{
....
}
}
And the logic explenation: You create a class (SubscribeToQueuePollingHandlerComponentComposer from the example) and define its base class to be ComponentComposer<Class_that_inherits_IComponent>.
And when you start the application you could see that it gets to the registered class' constractor (SubscribeToQueuePollingHandler constructor).
That's the way that I found to be able to call a function right when the application starts without needing to call its class constractor and actualy use dependency injection.

How to Create a Global Function then use on Controller

Im From VB.net base want to learn ASP.NET MVC
For Example : Function created below (how to create in global folder/cs and how to call it then use in Controller)
Function pRound(Number ,NumDigits)
Dim dblPower, vPSTEmp, intSgn
dblPower = 10 ^ NumDigits
vPSTEmp = CDbl(Number * dblPower + 0.5)
pRound = Int(vPSTEmp) / dblPower
End Function
For vb I just add <--#include file="include/function.asp"-->
then can use it like pRound(number, 4)
Please Teach Me How to Do it. Thx a lot.
You could add a new class file in your solution and make a static class;
namespace ProjectName.Functions
{
public static class Utility
{
public static float pRound(float number, int digits){
float result = 0;
// your code here
return result;
}
}
}
Then in your controller, since a static class is instantiated at the start of the program, you could just call it;
using ProjectName.Functions;
public ActionResult TestController
{
// call Utility.pRound(), no need to instantiate the class
float round = Utility.pRound(1,1);
}
I've edited Jerdine Saibo's answer.
By the time it gets approved, here's an updated code. (pRound methods needs to be static)
namespace ProjectName.Functions
{
public static class Utility
{
public static float pRound(float number, int digits){
float result = 0;
// your code here
return result;
}
}
}
You can make a wrapper class and call from the controller else another option is that you can create a base controller and implement it in all your other controller, then you can access like base.pRound(1,1);

AutoFac Register confusion

Hi I am just been looking at AutoFac and following their getting-started tutorial
http://autofac.readthedocs.org/en/latest/getting-started/index.html
having followed it and understanding how their services work I wanted to try to create a new implementation on the same interface type
builder.RegisterType<TodayWriter>().As<IDateWriter>();
builder.RegisterType<TomorrowWriter>().As<IDateWriter>();
Both implentations contain the same code
public class TomorrowWriter : IDateWriter
{
private IOutput _output;
public TomorrowWriter(IOutput output)
{
this._output = output;
}
public void WriteDate()
{
this._output.Write(DateTime.Today.AddDays(1).ToShortDateString());
}
}
So TodaysWriter is the same apart from the WriteDate method displaying
this._output.Write(DateTime.Today.ToShortDateString());
instead.
So now using the application, how do I determine what implementation to use as both methods are called WriteDate()
using(var scope = Container.BeginLifetimeScope())
{
var writer = scope.Resolve<IDateWriter>();
// Is this using todaysWriter or TomorrowWriter?
writer.WriteDate();
}
Am I using this wrong?
Thanks
To differentiate between different implementations of the same interface look at named and keyed services in the docs.
Alternatively you can roll your own by registering a DateWriterFactory and having a method on that to get a specific IDateWriter implementation. something like:
public class DateWriterFactory
{
IDateWriter GetWriter(string writerName)
{
if (writername=="TodayWriter")
return new TodayWriter();
if (writername=="TomorrowWriter")
return new TomorrowWriter();
}
}
obviously the implementation of the factory could be as complex or as simple as you need. Or you could just have methods to get the fixed writers rather than pass in a string.

Some classes called +_closure$_x shows up in my ASP.NET MVC projects - what are they?

When adding a new strongly typed view in ASP.NET MVC, these weird classes show up:
What are they?
Those are compiler-generated classes that handle closures. They're not unique to ASP.NET MVC.
This class will cause the compiler to generate one of these classes:
public class Foo
{
private bool _bar = true;
public Func<bool> HelloClosure()
{
return () => _bar;
}
}
When someone outside of Foo calls HelloClosure, they get back a function that has a link back to that particular Foo instance. Imagine we don't execute that function immediately and the GC comes along and collects Foo. Now what happens when we execute the function?
var fooInstance = new Foo();
var func = fooInstance.HelloClosure();
fooInstance = null;
GC.Collect();
// assuming fooInstance is collected
var result = func.Invoke();
These automatically-generated classes manage these dependencies between functions and instances so that we don't get into a situation like this.

Resources