Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java equivalent to C# ExpandoObject

Tags:

java

c#

C# code example:

dynamic MyDynamic = new System.Dynamic.ExpandoObject();
MyDynamic.A = "A";
MyDynamic.B = "B";
MyDynamic.C = "C";
MyDynamic.Number = 12;
MyDynamic.MyMethod = new Func<int>(() => 
{ 
    return 55; 
});
Console.WriteLine(MyDynamic.MyMethod());

Java: ?

Any ideas why java doesn't have support for this scenario?

like image 924
User1234 Avatar asked Feb 14 '16 12:02

User1234


People also ask

What is the equivalent of typedef of C++ in Java?

In one line, There is nothing in Java which is equivalent to typedef of C++. In Java, class is used to name and construct types or we can say that class is the combined function of C++’s struct and typedef. But that is totally different thing and not the equivalent of typedef anywhere.

What is the difference between C vs Java?

So from the above discussion, we came to the conclusion that C vs Java both are two different programming languages. Java is the most popular language at the industry level for the development of web applications as well as mobile applications. But we can’t deny the fact that C being the oldest one, is also a very popular language.

Is there a Java equivalent of instanceof in C++?

...... There is no direct equivalent in C++ to the Java 'instanceof' operator, but you can replicate the behavior by testing the result of a 'dynamic_cast':

What is the Java equivalent of anonymous inner classes in C++?

The closest equivalent to Java anonymous inner classes in C++ is to use a private class which implements the corresponding interface (but if the interface is a functional interface, then the closest equivalent is to replace the functional interface with a function pointer and the anonymous inner class with a lambda).


1 Answers

Java is much more strict in this case. So the short answer is no, Java doesn't have an Expando. The syntax just doesn't support that.

However there is an Expando in Groovy which is a dynamic language on top of Java.

BTW, If you're using Expando for tests, there are a lot of various Mock related solutions: EasyMock, Mockito, JMock to name a few.

like image 53
Mark Bramnik Avatar answered Oct 16 '22 20:10

Mark Bramnik