Interface implementation issue when call function of perticular interface - c#-2.0

How could I access I1 and I2 interfaces methods through I3 interface. I want to implement I3 in TestIt class (public class TestIt:I3). Do not want to implement I1 and I2 (public class TestIt:I1,I2).
My code is as below...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for TestIt
/// </summary>
public class TestIt:I3
{
public TestIt()
{
//
// TODO: Add constructor logic here
//
//TestItB tb = new TestItB();
}
public int abc()
{
return 10;
}
int I1.A1()
{
return 20;
}
void I2.A1()
{
int j = 0;
}
}
public interface I1
{
int A1();
}
public interface I2
{
void A1();
}
public interface I3 : I1, I2
{
}
And I try to call A1 method as below...
I3 t = new TestIt();
int i = t.A1();
above is throw error...
I do not want to create object as blow...
I1 t = new TestIT();
or
I2 t = new TestIT();

Cast the object to the appropriate interface type:
int i = ((I1)t).A1();
You can also do with with the as operator with (t as I1).A1(), although IMO casting gives a clearer indication of what's going on.

Related

Why does F# compiler prefer to generate closed implementations of FSharpFunc types?

For this code:
module Module =
let func x y z = 0
[<EntryPoint>]
let main args =
func 1
func 1 1
0
Decompilation yields:
[CompilationMapping(SourceConstructFlags.Module)]
public static class Main
{
[CompilationMapping(SourceConstructFlags.Module)]
public static class Module
{
[Serializable]
internal sealed class main#30 : OptimizedClosures.FSharpFunc<object, object, int>
{
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
[CompilerGenerated]
[DebuggerNonUserCode]
public int x;
[CompilerGenerated]
[DebuggerNonUserCode]
internal main#30(int x)
{
this.x = x;
}
public override int Invoke(object y, object z)
{
return func(x, y, z);
}
}
[Serializable]
internal sealed class main#31-1 : FSharpFunc<object, int>
{
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
[CompilerGenerated]
[DebuggerNonUserCode]
public int x;
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
[CompilerGenerated]
[DebuggerNonUserCode]
public int y;
[CompilerGenerated]
[DebuggerNonUserCode]
internal main#31-1(int x, int y)
{
this.x = x;
this.y = y;
}
public override int Invoke(object z)
{
return func(x, y, z);
}
}
[CompilationArgumentCounts(new int[]
{
1,
1,
1
})]
public static int func<a, b, c>(a x, b y, c z)
{
return 0;
}
[EntryPoint]
public static int main(string[] args)
{
int x = 1;
new main#30(x);
int x2 = 1;
int y = 1;
new main#31-1(x2, y);
return 0;
}
}
public static a Dump<a>(a arg00)
{
return arg00.Dump();
}
}
It generates a concrete type, that is generic parameters are provided at type definition. Why is not this done at the point of construction? I also noticed that types are generated in the module where call occurs, not where func is defined.
Having let func x y z = ... we need implementations of types to cover all possibilities:
FSharpFunc<T1,FSharpFunc<T2,T3,TReturn>>
FSharpFunc<T1,T2,FSharpFunc<T3,TReturn>>
FSharpFunc<T1,FSharpFunc<T2,FsharpFunc<T3,TReturn>>>
Compiler could generate all possible combinations in the same place, where function is defined, closing only for parameters with inferenced types.
You could argue that for the list of 7 args the set of types going to be quite large, but types like FSharpFunc<T1,T2,..,Tn, FSharpFunc<...>> are a mere optimazation. And FSharpFunc supports up to six generic types, then compiler has to switch to FSharpFun<T1,T2,T3,T4,T5,FSharp<...>>.
As pointed out by Fyodor it's not function creation that makes the compiler generating the hidden classes. The hidden classes are used to implement partial application.
In F# a partial application and lambdas are implemented as a compiler generated class that extends an abstract class. C# lambdas rely on delegates instead. IIRC Java and Scala use a similar technique to F# as JVM doesn't have delegates.
I suspect the F# compiler generates a class per partial application because it's simpler than collecting all partial applications and coalesce the identical ones.
It also helps the debuggability of F# programs as the name hints where the partial application was done: main#31-1 => In the main function at row 31. This name if included in logs or performance runs can help identifying what partial application is causing problems.
This comes at the cost of increasing the size of the F# assembly file as noted in a comment by Pavel.

Dependency injection issue (bidirectional communication)

I have a an instance of A and b an instance of B
a must be able to call a method on b and b must then immediatly call a method on a if some checks pass.
To achieve this I would have cyclic DI
public A(B b) { _b = b; }
public void CallToB() { _b.Method(); }
public void Method() { DoSomething(); }
public B(A a) { _a = a; }
public void Method() { if (SomeCheck()) _a.Method(); }
I know I could get arround this, using events and let b be unaware/independant of a. But it would feel wrong.
Note: I haven't seen an answer to this question where bidirectional communication was made possible.
You can solve this issue by depending on interfaces instead of concrete types and then use property injection. Here is an example:
public interface IA
{
void Method();
}
public class A : IA
{
private readonly IB _b;
public A(IB b){_b = b;}
//...
}
public interface IB
{
void Method();
}
public class B : IB
{
private readonly IA _a;
public B(IA a){_a = a;}
//...
}
public class BCycleDependencyBreaker : IB
{
private IB _b;
public IB b
{
set { _b = value; }
}
public void Method()
{
_b.Method();
}
}
You then use BCycleDependencyBreaker when you compose like this:
var b_cycle_dependency_breaker = new BCycleDependencyBreaker();
//Make a depend on this implementation of b that currently does nothing
A a = new A(b_cycle_dependency_breaker);
//Make b depend on a
B b = new B(a);
//Now, let the proxy implementation delegate calls to the real b
b_cycle_dependency_breaker.b = b;

How to return user defines class in JMX?

I am new to JMX and now I wished to monitor my project with JMX. Is there any way to return user defined class via MBeans/MXBean? I know that OpentType can help but don't know how to used it. I also went through Composite and Tabular data types but it may not work for me because I need to convert each and every class into respective data types.
Please provide your help.
Thank you in advance!!
You have to create an Interface SomethingMBean and a class that implement that interface.
public interface HelloMBean {
public void sayHello();
public int add(int x, int y);
public String getName();
public int getCacheSize();
public void setCacheSize(int size);
}
public class Hello ...
implements HelloMBean {
public void sayHello() {
System.out.println("hello, world");
}
public int add(int x, int y) {
return x + y;
}
public String getName() {
return this.name;
}
public int getCacheSize() {
return this.cacheSize;
}
public synchronized void setCacheSize(int size) {
...
this.cacheSize = size;
System.out.println("Cache size now " + this.cacheSize);
}
...
private final String name = "Reginald";
private int cacheSize = DEFAULT_CACHE_SIZE;
private static final int
DEFAULT_CACHE_SIZE = 200;
}
And then, you have to register your MBean...
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
ObjectName name = new ObjectName("com.example:type=Hello");
Hello mbean = new Hello();
mbs.registerMBean(mbean, name);
Take a look at Java Tutorial
If your project is using Spring, then it is quite easy to expose any user-defined class to JMX by simply using annotations:
#ManagedResource - to expose the class to JMX
#ManagedAttribute - to expose any fields of a ManagedResource class as attributes
#ManagedOperation - to expose any method of a ManagedResource class
as an operation
Have a look at the official Spring documentation which contains quite a few illustrative examples as well.

Converting a Foo into a Foo.ByValue?

I am integrating with an aged lm_sensors library using JNA and JNAerator, with a view to creating MBeans for each of the temperature sensors inside my box. Firstly I'm calling this method:
// C edition
const sensors_chip_name *sensors_get_detected_chips(int *nr);
// Java edition
sensors_chip_name sensors_get_detected_chips(IntByReference nr);
.. which works just fine. Subsequently I need to call:
// C edition
int sensors_get_feature(sensors_chip_name name, int feature, double *result);
// Java edition
int sensors_get_feature(sensors_chip_name.ByValue name, int feature, DoubleByReference result);
.. what I am lacking is how to take the result of sensors_get_detected_chips and pass it by value to the 1st argument of sensors_get_feature.
The following allows a ByValue version of the struct to be initialized from the base class.
public class sensors_chip_name extends Structure {
public class ByValue extends sensors_chip_name implements Structure.ByValue {
public ByValue(sensors_chip_name orig) {
this(orig.getPointer().share());
}
public ByValue(Pointer p) {
super(p);
}
public ByValue() { }
}
public sensors_chip_name() { }
public sensors_chip_name(Pointer p) {
super(p);
read();
}
}

Retrieving item text with JNA and SendMessage()

I am attempting to retrieve the item text from a Win32 ListView-like control. I am using JNA and SendMessageW() to send LVM_GETITEMTEXTW to the control. I have been successful at retrieving the item count (via LVM_GETITEMCOUNT) but am stumped at this point. My User32 class is setup like so:
public interface MyUser32 extends User32 {
MyUser32 INSTANCE = (MyUser32)Native.loadLibrary("user32", MyUser32.class);
LRESULT SendMessageW(HWND hWnd, int msg, WPARAM wParam, LVITEM lParam);
}
My LVITEM class is setup like so:
public class LVITEM extends Structure{
public LVITEM() {
pszText = new Memory(MEMSIZE);
cchTextMax = MEMSIZE;
}
private static final int MEMSIZE = 256;
public UINT mask;
public int iItem;
public int iSubItem;
public UINT state;
public UINT stateMask;
public Pointer pszText;
public int cchTextMax;
public int iImage;
public LPARAM lParam;
public int iIndent;
protected List<String> getFieldOrder() {
return Arrays.asList(new String[] { "mask", "iItem", "iSubItem", "state", "stateMask", "pszText", "cchTextMax", "iImage", "lParam", "iIndent"});
}
}
And the code that calls it all is like so:
MyUser32 u32 = MyUser32.INSTANCE;
LVITEM lvItem = new LVITEM();
WPARAM wPar = new WPARAM(1);
...
lvItem.iSubItem = 0;
res = u32.SendMessageW(handle, LVM_GETITEMTEXTW, wPar, lvItem);
System.out.println(res.intValue());
s = lvItem.pszText.getString(0);
System.out.println(s);
I've left out a bit of the code but I believe those are the important parts. My issue is that when I print out res.intValue() it is always 0 (meaning no text was returned) and when I print out the string value of pszText it is always some garbage characters. I'm completely stumped at this point so any suggestions are greatly appreciated. Thanks.

Resources