|
I am loading an assembly as follows:
Assembly assembly = assembly.LoadFile("file.dll");
I am then looping through the types within in and when a particular type is found I want to retrieve a property from it as follows:
Type[] types = null;
types = assembly.GetTypes();
foreach (Type type in types)
{
if(type.Name.ToString().Equals("TYPE"))
{
PropertyInfo proprty = type.GetProperty("PROPERTY", BindingFlags.GetProperty | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
}
}
The property is a HashTable but no matter what method I try to obtain the property value i.e GetValue etc I am unable to retrieve it.
I am probably doing something stupid. Can someone give me guidance as to the syntax required to obtain the property and cast it into an actual HashTable value that I can operate on?
|
|
|
|
|
First of all, you can't use the type until you instantiate it. Merely retrieving an assembly doesn't have any real effect. Secondly, why not use GetType instead of looping through the types? The following code should demonstrate some of what you need to do.
Assembly assembly = Assembly.LoadFile(@"pathtofile\TestFile.dll");
var type = assembly.GetType("TestFile.MyType");
if (type == null) return;
object instance = Activator.CreateInstance(type);
PropertyInfo propertyInfo = type.GetProperty("MyHashTable",
BindingFlags.GetProperty | BindingFlags.NonPublic |
BindingFlags.Public | BindingFlags.Instance);
if (propertyInfo != null)
{
object myHashTable = propertyInfo.GetValue(instance, null);
Hashtable ht = (Hashtable) myHashTable;
ht.Add("Hello", "Hello");
} Obviously, you'll want to put a lot more checking in place, but this should give you the basic idea.
|
|
|
|
|
Thanks for the info Pete.
That unfortunately is the trouble in Paradise because I am trying to use the properties of this assembly without referencing anything explicitly and the assembly in question has an entry point that is quite complex as it uses other referenced assemblies that connect to a database. plus there are lots of associated namespaces and associated functionality. a Framework - if you like...
Anyway - although it is a structured environment I want to do this dynamically. As I said without referencing. this is because this is a standalone application and I want to scan, not load the whole external app and its associated framework... Also if I do explicitly reference the required assemblies it will become a pain to manage further versions of my application as the external application it is supposed to reflect is a strongly typed application that is not backwards compatible i.e version 2 is not compatible with version 1 etc so each release will need to be recompiled...
I know it is possibly but it will just take me too much time to make it so at this point!
I am going to investigate the hashtable fields for key names... hopefully that will do it!! 
|
|
|
|
|
As Pete said it is impossible to read instance fields / properties without instantiating an object of that type. If you are trying to read static fields / properties though then you can do it like the following code sample I created:
using System;
using System.Collections.Generic;
namespace TestClassLibrary
{
public class TYPE
{
private static Dictionary<String, String> _PROPERTY = new Dictionary<String, String> { { "first", "First String" },
{ "second", "Second String" } };
public static Dictionary<String,String> PROPERTY { get { return _PROPERTY; } set { _PROPERTY = value; } }
}
}
This first code block is for the class library that will be loaded. The next code block will read the static property PROPERTY and will out put its values to the console:
using System;
using System.Collections.Generic;
using System.Reflection;
namespace TestReflection
{
class Program
{
static void Main(string[] args)
{
Assembly assembly = Assembly.LoadFile(@"D:\Development\Console\Playgroung\TestReflection\TestClassLibrary\bin\Debug\TestClassLibrary.dll");
Type type = assembly.GetType("TestClassLibrary.TYPE");
PropertyInfo propertyInfo = type.GetProperty("PROPERTY", BindingFlags.GetProperty | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
Dictionary<String, String> dictionary = (Dictionary<String, String>)propertyInfo.GetValue(null, null);
foreach (KeyValuePair<String, String> kv in dictionary)
{
Console.WriteLine("{0} => {1}", kv.Key, kv.Value);
}
}
}
}
Is this what you had in mind? Notice the BindingFlags.Static in the GetProperty call.
Regards,
— Manfred
"I had the right to remain silent, but I didn't have the ability!"
Ron White, Comedian
|
|
|
|
|
Hi,
I used the below connection string to read excel file.
connectionString = "Provider=Microsoft.Jet.OLEDB.12.0;Data Source=D:\test2.xlsx;Extended Properties=\'Excel 12.0;HDR=No;\";
While opening, an exception is thrown.. "External table is not in the expected format"
I explored in serveral sites and changed the connection string as mentioned there and followed other solutions. But none of them didn't work.
Please guide me to solve this issue. Thanks in advance.
|
|
|
|
|
|
I used the below connection string as mentioned in the given article. Still error exists
connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\test.xlsx;Extended Properties=\"Excel 12.0;HDR=NO\"";
|
|
|
|
|
You need to escape the backslash in your file name thus:
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\test.xlsx;Extended Properties=\"Excel 12.0;HDR=NO\"";
Use the best guess
|
|
|
|
|
I'm reading that data source value in runtime. so blackslash is not an issue.
|
|
|
|
|
I can only assume that there is something else causing the problem. Are you able to read the file with Excel?
Use the best guess
|
|
|
|
|
I followed below steps to make it work. Now, I'm able to read file
1. open your excel file
2. try to read that
3. Able to read the file
4. to read the file next time, no need to open the file.
I don't know the reason how and why its working 
|
|
|
|
|
I've an ItemsControl that is virtualized using following options (.NET 4.5):
VirtualizingStackPanel.IsVirtualizing="True" ScrollViewer.CanContentScroll="True" VirtualizingPanel.ScrollUnit="Pixel"
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
Scrolling using Pixels works but I noticed that the offset that i set is beeing modified afterwards, and that's a big issue for me hence i sync the offset with other controls. Also BringIntoView() does also behave wrong (randomly not work).
This is how it looks like when I log the offset I set and the offset reported in OnScrollChanged.
Set = 614,745
ScrollChanged = 614,745
Set = 702,565714285714
ScrollChanged = 702,565714285714
ScrollChanged = 658,838326118333
ScrollChanged = 681,27584415585
Btw, this don't happend if i use an ordinary StackPanel. Any idea why the ScrollViewer behaves this way?
|
|
|
|
|
Hi All,
I want to use the below powershell script in c#. The script is with two foreach loop and the 2nd foreach loop uses add-member to add the value that is part of $mailbox variable. I managed to write c# code to work upto first foreach. I need help to complete the c# code with 2nd foreach loop and using the value of $mailbox.
Powershell Script :
ForEach ($Mailbox in Get-Mailbox) {Get-ActiveSyncDeviceStatistics -Mailbox $Mailbox.Identity –ErrorAction SilentlyContinue | Select DeviceFriendlyName, Devicetype, DeviceUserAgent | ForEach-Object { $_ | Add-Member –MemberType NoteProperty -Name "MailboxIdentity" -value $Mailbox}}
I managed to go upto,
c# code:
PowerShellpowershell = PowerShell.Create();
PSCommandcommand = newPSCommand();
command.AddCommand("Get-Mailbox");
command.AddCommand("where-object");
command.AddParameter("Filterscript", scriptBlock.Create("!$_.name.startswith(\"DiscoverySearchMailbox\")"));
powershell.Commands = command;
powershell.Runspace = CreateRunSpace.GetRunSpace();
varresult = powershell.Invoke();
PSCommandcommand1 = newPSCommand();
command1.AddCommand("write-output");
command1.AddParameter("InputObject", result);
command1.AddCommand("Foreach-Object");
command1.AddParameter("Process", ScriptBlock.Create("Get-ActiveSyncDeviceStatistics -Mailbox $_.Identity"));
powershell.Commands = command1;
powershell.Runspace = CreateRunSpace.GetRunSpace();
var result1 = powershell.Invoke();
I'm stuck at 2nd foreach loop to add-member while adding the output of get-mailbox. Appreciate your help and suggestions. Thank you.
|
|
|
|
|
You already asked these two questions in Quick Answers, please use one forum only.
Use the best guess
|
|
|
|
|
Hi All,
I want to use the below powershell command in c# and get the desired output.
Powershell script:
foreach($a in get-mailbox){get-activesyncdevicestatistics -mailbox $a.identity | Select DeviceFriendlyName, Devicetype, DeviceUserAgent,@{Name="User Name";expression={$a.displayname}}}
------------------------------------------------------------------------
I have made the code as below, but need your help to work on Select part with hash table. Please let me know, how I can include the "Select DeviceFriendlyName, Devicetype, DeviceUserAgent,@{Name="User Name";expression={$a.displayname}}}" in the below code.
----------------------------------------------------------------------------
C# code:
Collapse | Copy Code
PowerShellpowershell = PowerShell.Create();
PSCommandcommand = newPSCommand();
command.AddCommand("Get-Mailbox");
command.AddCommand("where-object");
command.AddParameter("Filterscript", scriptBlock.Create("!$_.name.startswith(\"DiscoverySearchMailbox\")"));
powershell.Commands = command;
powershell.Runspace = CreateRunSpace.GetRunSpace();
varresult = powershell.Invoke();
PSCommandcommand1 = newPSCommand();
command1.AddCommand("write-output");
command1.AddParameter("InputObject", result);
command1.AddCommand("Foreach-Object");
command1.AddParameter("Process", ScriptBlock.Create("Get-ActiveSyncDeviceStatistics -Mailbox $_.Identity"));
powershell.Commands = command1;
powershell.Runspace = CreateRunSpace.GetRunSpace();
var result1 = powershell.Invoke();
|
|
|
|
|
I have rdlc string in which some words are bold,have text color,italic,etc and this string i get from database and i need to display it in rdlc report as is formated.
|
|
|
|
|
Assuming the field is formatted with HTML, you should just need to set the "Markup type" property on the field to "HTML":
http://msdn.microsoft.com/en-us/library/dd207057.aspx[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Thank you for your help bu unfortunately I have designed report in VS.NET 2008 and currently i need solution for version VS.NET 2008 before i plan to migrate my report in newer version.
|
|
|
|
|
If your report is running against SQL Server 2005, you can't use inline formatting.
If it's running against SQL Server 2008 or higher, the "Markup type" property should work. Depending on your version of BIDS, you might need to open the report in Report Builder to access the property.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Good Afternoon
my name is varun kumar.
i am very new in software developement .
i want to know that how can it get the default home page address url of any browser installed on my computer.
like IE. default/home page url is :www.google.com
so how can i get this url on c# windows application on button click.
please help me
Thanks and Regards
varun kumar
|
|
|
|
|
This is stored in the registry in the HKEY_CURRENT_USER hive. Where depends on the browser. Every browser will store this in a different location. This means you're going to have to research on every browser you want to support.
Once you have that information you just use the standard Registry class to get the values.
|
|
|
|
|
how to control Speech Recognition Engine using c# code.
and i want to show all command in text box which i said.
|
|
|
|
|
|
Check this google result[^]
I Love T-SQL
"VB.NET is developed with C#.NET"
If my post helps you kindly save my time by voting my post.
|
|
|
|
|
Wow, it's as if there is a vast network of interconnected computers (an Inter-Net if you will) which contains vast amounts of information and pages which are woven together across the planet through links (a world-wide web of documents if you will) which can be accessed via the results of some sort of easy to use information finding machine (or search-engine if you will).
It is a pity the OP isn't aware of these Earth-shattering revelations!
[Edit]
I'm not sure if you down-voted Blue_Boy, but the sarcasm wasn't directed at you, it's just obvious the OP hasn't even tried Google. Down-voting for sarcasm totally legit though.
modified 26-Jul-13 6:48am.
|
|
|
|