How do I create an object?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is there a way to instantiate an object whose type isn't known until runtime?
Something comparable to Java's Class.newInstance()?
Thanks,
- Jim Robertson
 
Jim Robertson said:
Is there a way to instantiate an object whose type isn't known until
runtime?
Something comparable to Java's Class.newInstance()?

Yup. Check the docs for Activator.CreateInstance().

Just btw, like Java, .Net supports reflection so you'll be able to get an
instance of a class if you like and inspect it for methods and members as
well, if you have a need to do that.

Regards,
Will
 
Hi,
on run time, you can get the name (if possible) then you can use,
Type formType = Type.GetType("System.Windows.Forms.Form");
Then,

Object obj = Activator.createInstance(formType);


The .Net reflection apis are almost same as java reflection apis.

HTH,
Mahesh Devjibhai Dhola
Microsoft MVP
 
I have the almost the same question, but it's regarding a web service
object. The URL is known, so I think I can avoid messing with UDDI, but I
want to discover the methods (specifically the parameters to a known method)
at run time. The idea is this: Suppose a particular web service exposes a
Person object. It has a method such as SetInfo(int age, string firstName,
string lastName). I have a collection of personal info from another source
that I want to pass to this web service. The info I have available incudes
age, firstName, middleName, lastName, maidenName, nameSuffix and SSN. The
web service will eventually be changed to accept some of the additional
information I have available and when it does, I want to pass them to the
corresponding parameters without recompiling my code. For example, SetInfo
may be changed to look like this: SetInfo(int age, string firstName, string
middleName, string lastName, string SSN). In that case, I want to "discover"
the parameter names at run time and provide the corresponding values from my
available data. If the Person object was a "normal" .NET component, I would
know how to use reflection to learn the methods and properties at run time
and to invoke them. If the Person.SetInfo method was a stored procedure, I
could loop through the parameters collection and learn their names. I
haven't worked with .NET remoting yet, so I don't know if that's even
applicable with a web service. How can I do something comparable with a web
service object?

Thanks,
Dave
 
The webservice exposes a WSDL document for that vary purpose. Append wsdl to
the end of the URL and it will return the valid schema for the webservice.
You can simply query the returned XML to find what you need without
resorting to reflection and the like.

From a design perspective, that's a horrid idea because
it imposes a cost on a call to a webservice which is equal to probing and
retrieving parameters before making the actual call. Added to the turtle
performance of an XML webservice and you have a recipe for disaster in high
concurrency situations. A better approach is to program to the final
webservice API since its implementation should be set in stone after
release.

--
Regards,
Alvin Bruney

[Shameless Author Plug]
The Microsoft Office Web Components Black Book with .NET
available at www.lulu.com/owc
 
I've looked at the WDSL document and I know it can provide the info I need.
It's a bit of a hassle to paste together all of the .NET components to
retreive the XML document via http then load it onto an XmlDocument so I can
navigate to the desired XmlNodes which specify the parameters to the Soap
method. I was hoping that there was another component already in the
framework which would handle much of this under the hood. I was envisioning
something like the way I would learn the parameter names for a local object
by retreiving a ParameterInfo array from System.Reflection, except that in
this case, I'd need to specify specify the Soap URL in addition to the type
name. It would work something roughly like this:

To get the parameter list for Person.SetInfo() method:

string soapUrl = http://Myweb.com/MyWebService/Person.asmx"
string soapMethodName = "SetInfo"
ParameterInfo[] params = GetSoapParameters(soapUrl, soapMethodName)
foreach(ParameterInfo param in params)
{
Console.WriteLine(param.ParameterType.Name + " " + param.Name);
}

As far as the performance hit is concerned, I understand that it would be
terrible if I did this with each call to the web service, but in practice, I
plan to do it only once when my application first starts, at which time, I
would maintain the params collection and use it each time I need to invoke
SetInfo. The web service interface isn't likely to be set in stone any time
soon, but it will probably change many times in the future. I don't want to
maintain the code that interfaces to it on an ongoing basis. That's why I'm
trying to make my code smart enough to work with any foreseeable changes in
the web service, which at this point consists only of changes to the names
and data types of particular parameters to SetInfo(). I've done similar
things with caching the parameters collection for a stored procedure at run
time, then looping through the parmeter names to determine which data I
needed to provide. That way, the stored procedure could be changed as needed
to facilitate new parameters. It's a pretty effective design as long as you
cache the parameter list once rather than retreiving it each time.

If you have any ideas about how to implement the GetSoapParameters() method
in the example above, I'd appreciate any suggestions.

Thanks,
Dave

Alvin Bruney said:
The webservice exposes a WSDL document for that vary purpose. Append wsdl to
the end of the URL and it will return the valid schema for the webservice.
You can simply query the returned XML to find what you need without
resorting to reflection and the like.

From a design perspective, that's a horrid idea because
it imposes a cost on a call to a webservice which is equal to probing and
retrieving parameters before making the actual call. Added to the turtle
performance of an XML webservice and you have a recipe for disaster in high
concurrency situations. A better approach is to program to the final
webservice API since its implementation should be set in stone after
release.

--
Regards,
Alvin Bruney

[Shameless Author Plug]
The Microsoft Office Web Components Black Book with .NET
available at www.lulu.com/owc
------------------------------------------------------------

Dave Hall said:
I have the almost the same question, but it's regarding a web service
object. The URL is known, so I think I can avoid messing with UDDI, but I
want to discover the methods (specifically the parameters to a known
method)
at run time. The idea is this: Suppose a particular web service exposes a
Person object. It has a method such as SetInfo(int age, string firstName,
string lastName). I have a collection of personal info from another source
that I want to pass to this web service. The info I have available incudes
age, firstName, middleName, lastName, maidenName, nameSuffix and SSN. The
web service will eventually be changed to accept some of the additional
information I have available and when it does, I want to pass them to the
corresponding parameters without recompiling my code. For example, SetInfo
may be changed to look like this: SetInfo(int age, string firstName,
string
middleName, string lastName, string SSN). In that case, I want to
"discover"
the parameter names at run time and provide the corresponding values from
my
available data. If the Person object was a "normal" .NET component, I
would
know how to use reflection to learn the methods and properties at run time
and to invoke them. If the Person.SetInfo method was a stored procedure, I
could loop through the parameters collection and learn their names. I
haven't worked with .NET remoting yet, so I don't know if that's even
applicable with a web service. How can I do something comparable with a
web
service object?

Thanks,
Dave
 
Thanks! That's what I was looking for.
I hope the new parametrized type feature of C# will also have reflection
support.
-- Jim Robertson
 
I tried instantiating a generic class dynamically, but it didn't work. I
wanted to accomplish, dynamically, the same thing that is done statically with
System.ServiceModel.ServiceHost<Foo> fooService = new
System.ServiceModel.ServiceHost<Foo>(myURI);

I tried the following:
string typeName = "System.ServiceModel.ServiceHost`1[myNameSpace.Foo]";
Type myType = Type.GetType(typeName);
Object obj = Activator.CreateInstance(myType, args);

but the call, Type.GetType(typeName), returned null.

-- Jim Robertson
 
Back
Top