可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
This is what I'm trying to do:
private KinectAudioSource CreateAudioSource()
{
var source = KinectSensor.KinectSensors[0].AudioSource;
source.NoiseSuppression = _isNoiseSuppressionOn;
source.AutomaticGainControlEnabled = _isAutomaticGainOn;
return source;
}
private object lockObj = new object();
private void RecordKinectAudio()
{
lock (lockObj)
{
using (var source = CreateAudioSource())
{
}
}
}
The 'using' statement gives one error which states:
'Microsoft.Kinect.KinectAudioSource':type used in a using statement must be implicitly convertible to 'System.IDisposable'
How do I eliminate this error and what does it mean?
回答1:
I am very late to the party, but I should say:
You must add a reference to Entity Framework
if you get this error while using context inside using statement.
回答2:
You can create like that :
public class HelloTest : IDisposable
{
void IDisposable.Dispose()
{
}
public void GetData()
{
}
}
after then you can able create object like
using (HelloTest Ht = new HelloTest())
{
Ht.GetData();
}
I hope above example helpfull
回答3:
I had a similar problem when creating a new project I had forgotten to install ENTITY FRAMEWORK via Nuget Package Manager. Sorry if this doesn't relate to kinect but this is where google took me when I was looking for the error in VS.
回答4:
Using
keyword required IDisposable
interface implementation. If you are getting error 'Microsoft.Kinect.KinectAudioSource':type used in a using statement must be implicitly convertible to 'System.IDisposable.
So it means like Joachim said KinectAudioSource
is not IDisposable
.
Instead of using
you can use
try
{
Execute();
}
finally
{
CleanupPart();
}
using
is equivalent try-finally
. You will only use try-finally
when you want to do some clean up inside finally and don't care about the exception.
回答5:
The class KinectAudioSource
is supposed to implement the IDisposable
interface in order to be using with using block. The classes that do not implement Idisposable could not be instantiated in using statement.
As a rule, when you use an IDisposable objAs a rule, when you use an
IDisposable object, you should declare and instantiate it in a using
statement. The using statement calls the Dispose method on the object
in the correct way, and (when you use it as shown earlier) it also
causes the object itself to go out of scope as soon as Dispose is
called. Within the using block, the object is read-only and cannot be
modified or reassigned, MSDN
回答6:
KinectAudioSource
is not IDisposable
, so it cannot be used in a using
block. What you probably mean to do is to close the data stream (which does implement IDisposable) instead when you're done recording, something like;
private Stream CreateAudioStream()
{
var source = KinectSensor.KinectSensors[0].AudioSource;
source.NoiseSuppression = _isNoiseSuppressionOn;
source.AutomaticGainControlEnabled = _isAutomaticGainOn;
return source.Start();
}
private object lockObj = new object();
private void RecordKinectAudio()
{
lock (lockObj)
{
using (var source = CreateAudioStream())
{
}
}
}
回答7:
Also adding .NET reference from System.EnterpriseServices version 2 will solve the error. This is especially useful if you are converting from and older version and you have multiple occurrences of "using" keyword to replace
回答8:
The System.IDisposable error occurs because the connection you are trying to open might not close automatically, when out of scope of where the connection was opened.
Exclude your model connection creation from the using() clause, so that it just remains as :
var source = new CreateAudioSource();
/*
Rest of code here
*/
Also make sure that you won't ommit the 'new' keyword for the object creation.
回答9:
I had a similar problem when creating a new console application .I had forgotten to add ENTITY FRAMEWORK reference in my project.
Adding reference on ENTITY FRAMEWORK resolve issue.
回答10:
You should add System.Data.Linq
to your References in the project. This solved the problem for me.